Skip to content
This repository was archived by the owner on Feb 10, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 38 commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
8fe7434
Implement LinkedQueue.java
tboychuk Jul 31, 2018
f5a4dde
Fix logic to follow FIFO rule
tboychuk Aug 1, 2018
1478575
Merge branch 'master' into exercise/completed
tboychuk Aug 1, 2018
79f73f3
Merge branch 'master' into exercise/completed
tboychuk Aug 1, 2018
3b46dca
Merge branch 'master' into exercise/completed
tboychuk Aug 1, 2018
e0fdefe
Implement exercise concurrent-linked-queue-simple
tboychuk Aug 1, 2018
a206bc0
Merge branch 'master' into exercise/completed
tboychuk Aug 1, 2018
6195fcb
Merge branch 'master' into exercise/completed
tboychuk Aug 1, 2018
755139b
Merge branch 'master' into exercise/completed
tboychuk Aug 2, 2018
ed6ab0e
Simplify class Node.java as a pure data structure, and hide it
tboychuk Aug 7, 2018
f2836e2
Merge branch 'master' into exercise/completed
tboychuk Aug 7, 2018
210e2f6
Complete exercise
tboychuk Aug 7, 2018
ba95354
Merge branch 'master' into exercise/completed
tboychuk Aug 7, 2018
d7007bf
Merge branch 'master' into exercise/completed
tboychuk Aug 9, 2018
c7f1d80
Fix method add(int index, T element)
tboychuk Aug 9, 2018
fffd4df
Fix index at method remove()
tboychuk Aug 9, 2018
791acad
Add comment for method findTail()
tboychuk Aug 9, 2018
0f93579
Merge branch 'master' into exercise/completed
tboychuk Aug 13, 2018
75ba68c
Fix LinkedList#set()
tboychuk Aug 13, 2018
1569048
Merge branch 'master' into exercise/completed
tboychuk Aug 14, 2018
8da56dd
Complete FileStats.java exercise
tboychuk Aug 14, 2018
0c82e41
Merge branch 'master' into exercise/completed
tboychuk Aug 14, 2018
0cca6c7
Remove redundant code from LinkedList#set()
tboychuk Aug 14, 2018
6ec5e02
Simplify LinkedList#add(index, element)
tboychuk Aug 14, 2018
4cbe0f5
Merge branch 'master' into exercise/completed
tboychuk Aug 15, 2018
cfc3847
Complete exercises
tboychuk Aug 15, 2018
9105439
Merge branch 'master' into exercise/completed
tboychuk Aug 15, 2018
461f803
Merge branch 'master' into exercise/completed
tboychuk Aug 16, 2018
0d0cd0e
Merge branch 'master' into exercise/completed
tboychuk Aug 29, 2018
8505265
Merge branch 'master' into exercise/completed
tboychuk Oct 23, 2018
89a43cb
Upgrade LinkedList.java implementation
tboychuk Oct 23, 2018
1392087
Merge branch 'master' into exercise/completed
tboychuk Oct 24, 2018
ee86666
Merge branch 'master' into exercise/completed
tboychuk Oct 24, 2018
c9f0244
Merge branch 'master' into exercise/completed
tboychuk Oct 24, 2018
20c1aaf
Merge branch 'master' into exercise/completed
tboychuk Oct 24, 2018
da6aa0b
Upgrade LinkedList.java
tboychuk Oct 25, 2018
9434e3d
Merge branch 'master' into exercise/completed
tboychuk Oct 25, 2018
afc5479
Merge branch 'master' into exercise/completed
tboychuk Oct 25, 2018
4370c02
Update FileReaders.java
tboychuk Oct 26, 2018
c0fa442
Upgrade the implementation of LinkedList.java
tboychuk Oct 27, 2018
36c2348
Upgrade the implementation of LinkedList.java
tboychuk Oct 27, 2018
bb74678
Upgrade the implementation of LinkedList.java
tboychuk Oct 27, 2018
4b82d55
Merge branch 'master' into exercise/completed
tboychuk Oct 30, 2018
7f13124
Merge branch 'master' into exercise/completed
tboychuk Jul 1, 2019
7bba9c0
#6 add java functional features exercises with solutions
Shtramak Jul 1, 2019
96b5af3
Merge pull request #10 from bobocode-projects/ticket/#6-completed
tboychuk Jul 2, 2019
249b5cf
Merge branch 'master' into exercise/completed
tboychuk Jul 2, 2019
92524fa
Merge branch 'master' into exercise/completed
tboychuk Jul 2, 2019
3ee80b1
Implement new methods is CrazyLambdas.java
tboychuk Jul 2, 2019
73beb18
Merge branch 'master' into exercise/completed
tboychuk Jul 26, 2019
b554fdc
Merge branch 'master' into exercise/completed
tboychuk Aug 15, 2019
4068f38
GP-4 Create new exercise CrazyOptionals.java and upgrade Accounts.java
tboychuk Aug 14, 2019
7cdc096
Merge pull request #16 from bobocode-projects/GP-4-CrazyOptionalExerc…
tboychuk Aug 15, 2019
9394eef
Merge branch 'master' into exercise/completed
tboychuk Apr 28, 2020
c9bbe03
Merge branch 'master' into exercise/completed
tboychuk Apr 28, 2020
606a6b5
Merge branch 'master' into exercise/completed
tboychuk Jul 16, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,29 +1,65 @@
package com.bobocode;

import java.util.concurrent.atomic.AtomicInteger;

/**
* This queue should be implemented using generic liked nodes. E.g. class Node<T>. In addition, this specific
* should be thread-safe, which means that queue can be used by different threads simultaneously, and should work correct.
*
* @param <T> a generic parameter
*/
public class ConcurrentLinkedQueue<T> implements Queue<T> {
static final class Node<T> {
private T element;
private Node<T> next;

static <T> Node<T> valueOf(T element) {
return new Node<>(element);
}

private Node(T element) {
this.element = element;
}
}

private Node<T> head;
private Node<T> tail;
private AtomicInteger size = new AtomicInteger();

@Override
public void add(T element) {
throw new UnsupportedOperationException("This method is not implemented yet"); // todo: implement this method
synchronized public void add(T element) {
Node<T> newNode = Node.valueOf(element);
if (head == null) {
head = tail = newNode;
} else {
tail.next = newNode;
tail = newNode;
}
size.incrementAndGet();
}

@Override
public T poll() {
throw new UnsupportedOperationException("This method is not implemented yet"); // todo: implement this method
synchronized public T poll() {
if (head != null) {
T element = head.element;
head = head.next;
if (head == null) {
tail = null;
}
size.decrementAndGet();
return element;
} else {
return null;
}
}

@Override
public int size() {
throw new UnsupportedOperationException("This method is not implemented yet"); // todo: implement this method
return size.get();
}

@Override
public boolean isEmpty() {
throw new UnsupportedOperationException("This method is not implemented yet"); // todo: implement this method
return head == null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.bobocode;

public class FileReaderException extends RuntimeException {
public FileReaderException(String message, Exception e) {
super(message, e);
}
}
34 changes: 33 additions & 1 deletion file-reader/src/main/java/com/bobocode/FileReaders.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
package com.bobocode;

import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Objects;
import java.util.stream.Stream;

import static java.util.stream.Collectors.joining;

/**
* {@link FileReaders} privides an API that allow to read whole file into a {@link String} by file name.
*/
Expand All @@ -12,6 +23,27 @@ public class FileReaders {
* @return string that holds whole file content
*/
public static String readWholeFile(String fileName) {
throw new UnsupportedOperationException("It's your job to make it work!"); //todo
Path filePath = createPathFromFileName(fileName);
try (Stream<String> fileLinesStream = openFileLinesStream(filePath)) {
return fileLinesStream.collect(joining("\n"));
}
}

private static Stream<String> openFileLinesStream(Path filePath) {
try {
return Files.lines(filePath);
} catch (IOException e) {
throw new FileReaderException("Cannot create stream of file lines!", e);
}
}

private static Path createPathFromFileName(String fileName) {
Objects.requireNonNull(fileName);
URL fileUrl = FileReaders.class.getClassLoader().getResource(fileName);
Copy link
Contributor

@shryhus shryhus Oct 25, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"getResource()"
Does this method retrieve a "resources" folder?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not only, in our case files from folder resources will be in the classpath, and that method allows to get a URL object of any kind of resource (any file) by its name.

You can check its javadoc for more details.

try {
return Paths.get(fileUrl.toURI());
} catch (URISyntaxException e) {
throw new FileReaderException("Invalid file URL",e);
}
}
}
74 changes: 70 additions & 4 deletions file-stats/src/main/java/com/bobocode/FileStats.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,76 @@
package com.bobocode;

import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Comparator;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Stream;

import static java.util.function.Function.identity;
import static java.util.stream.Collectors.counting;
import static java.util.stream.Collectors.groupingBy;

/**
* {@link FileStats} provides an API that allow to get character statistic based on text file. All whitespace characters
* are ignored.
*/
public class FileStats {
private final Map<Character, Long> characterCountMap;
private final char mostPopularCharacter;

/**
* Creates a new immutable {@link FileStats} objects using data from text file received as a parameter.
*
* @param fileName input text file name
* @return new FileStats object created from text file
*/
public static FileStats from(String fileName) {
throw new UnsupportedOperationException("It's your job to make it work!"); //todo
return new FileStats(fileName);
}

private FileStats(String fileName) {
Path filePath = getFilePath(fileName);
characterCountMap = computeCharacterMap(filePath);
mostPopularCharacter = findMostPopularCharacter(characterCountMap);
}

private Path getFilePath(String fileName) {
Objects.requireNonNull(fileName);
URL fileUrl = getFileUrl(fileName);
try {
return Paths.get(fileUrl.toURI());
} catch (URISyntaxException e) {
throw new FileStatsException("Wrong file path", e);
}
}

private URL getFileUrl(String fileName) {
URL fileUrl = getClass().getClassLoader().getResource(fileName);
if (fileUrl == null) {
throw new FileStatsException("Wrong file path");
}
return fileUrl;
}

private Map<Character, Long> computeCharacterMap(Path filePath) {
try (Stream<String> lines = Files.lines(filePath)) {
return collectCharactersToCountMap(lines);
} catch (IOException e) {
throw new FileStatsException("Cannot read the file", e);
}
}

private Map<Character, Long> collectCharactersToCountMap(Stream<String> linesStream) {
return linesStream
.flatMapToInt(String::chars)
.filter(a -> a != 32) // filter whitespace
.mapToObj(c -> (char) c)
.collect(groupingBy(identity(), counting()));
}

/**
Expand All @@ -22,7 +80,7 @@ public static FileStats from(String fileName) {
* @return a number that shows how many times this character appeared in a text file
*/
public int getCharCount(char character) {
throw new UnsupportedOperationException("It's your job to make it work!"); //todo
return characterCountMap.get(character).intValue();
}

/**
Expand All @@ -31,7 +89,15 @@ public int getCharCount(char character) {
* @return the most frequently appeared character
*/
public char getMostPopularCharacter() {
throw new UnsupportedOperationException("It's your job to make it work!"); //todo
return mostPopularCharacter;
}

private char findMostPopularCharacter(Map<Character, Long> characterCountMap) {
return characterCountMap.entrySet()
.stream()
.max(Comparator.comparing(Map.Entry::getValue))
.get()
.getKey();
}

/**
Expand All @@ -41,6 +107,6 @@ public char getMostPopularCharacter() {
* @return {@code true} if this character has appeared in the text, and {@code false} otherwise
*/
public boolean containsCharacter(char character) {
throw new UnsupportedOperationException("It's your job to make it work!"); //todo
return characterCountMap.containsKey(character);
}
}
Loading