Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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,45 +1,15 @@
package com.bobocode.intro;

import com.bobocode.util.ExerciseNotCompletedException;
import java.util.Base64;


/**
* Welcome! This is an introduction exercise that will show you a simple example of Bobocode exercises.
* <p>
* JavaDoc is a way of communication with other devs. We use Java Docs in every exercise to define the task.
* So PLEASE MAKE SURE you read the Java Docs carefully.
* <p>
* Every exercise is covered with tests, see {@link ExerciseIntroductionTest}.
* <p>
* In this repo you'll find dozens of exercises covering various fundamental topics.
* They all have the same structure helping you to focus on practice and build strong skills!
*
* @author Taras Boychuk
*/
public class ExerciseIntroduction {
/**
* This method returns a very important message. If understood well, it can save you years of inefficient learning,
* and unlock your potential!
*
* @return "The key to efficient learning is practice!"
*/

public String getWelcomeMessage() {
// todo: implement a method and return a message according to javadoc
throw new ExerciseNotCompletedException();
return "The key to efficient learning is practice!";
}

/**
* Method encodeMessage accepts one {@link String} parameter and returns encoded {@link String}.
* <p>
* PLEASE NOTE THAT YOU WILL GET STUCK ON THIS METHOD INTENTIONALLY! ;)
* <p>
* Every exercise has a completed solution that is stored in the branch "completed". So in case you got stuck
* and don't know what to do, go check out completed solution.
*
* @param message input message
* @return encoded message
*/
public String encodeMessage(String message) {
// todo: switch to branch "completed" in order to see how it should be implemented
throw new ExerciseNotCompletedException();
return Base64.getEncoder().encodeToString(message.getBytes());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
package com.bobocode.basics;

/**
* {@link Box} is a container class that can store a value of any given type. Using Object as a field type
* is flexible, because we can store anything we want there. But it is not safe, because it requires runtime casting
* and there is no guarantee that we know the type of the stored value.
* <p>
* todo: refactor this class so it uses generic type "T" and run {@link com.bobocode.basics.BoxTest} to verify it
*/
public class Box {
private Object value;

public Box(Object value) {
public class Box<T> {
private T value;

public Box(T value) {
this.value = value;
}

public Object getValue() {
public T getValue() {
return value;
}

public void setValue(Object value) {
public void setValue(T value) {
this.value = value;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
package com.bobocode.basics;

/**
* This demo demonstrates why using Object is not safe. It's not safe because runtime casting can cause runtime
* exceptions. We should always fail as soon as possible. So in this code we should not allow setting String
* value at compile time, if we expect to work with integers.
* <p>
* todo: refactor class {@link Box} to make type parameterization safe and make this demo fail at compile time
*/

public class BoxDemoApp {
public static void main(String[] args) {
Box intBox = new Box(123);
Box intBox2 = new Box(321);
Box<Integer> = new Box<>(123);
Box<Integer> = new Box<>(321);
System.out.println((int) intBox.getValue() + (int) intBox2.getValue());

intBox.setValue(222);
intBox.setValue("abc"); // this should not be allowed
// the following code will compile, but will throw runtime exception
intBox.setValue("abc");
System.out.println((int) intBox.getValue() + (int) intBox2.getValue());
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package com.bobocode.basics;

import com.bobocode.basics.util.BaseEntity;
import com.bobocode.util.ExerciseNotCompletedException;
import lombok.Data;
import lombok.val;

import java.io.Serializable;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.*;
import java.util.function.Predicate;

/**
* {@link CrazyGenerics} is an exercise class. It consists of classes, interfaces and methods that should be updated
Expand All @@ -33,8 +31,8 @@ public class CrazyGenerics {
* @param <T> – value type
*/
@Data
public static class Sourced { // todo: refactor class to introduce type parameter and make value generic
private Object value;
public static class Sourced<T> { // todo: refactor class to introduce type parameter and make value generic
private T value;
private String source;
}

Expand All @@ -45,11 +43,11 @@ public static class Sourced { // todo: refactor class to introduce type paramete
* @param <T> – actual, min and max type
*/
@Data
public static class Limited {
public static class Limited<T extends Number> {
// todo: refactor class to introduce type param bounded by number and make fields generic numbers
private final Object actual;
private final Object min;
private final Object max;
private final T actual;
private final T min;
private final T max;
}

/**
Expand All @@ -59,8 +57,8 @@ public static class Limited {
* @param <T> – source object type
* @param <R> - converted result type
*/
public interface Converter { // todo: introduce type parameters
// todo: add convert method
public interface Converter<T,R> { // todo: introduce type parameters
R convert(T obj); // todo: add convert method
}

/**
Expand All @@ -70,10 +68,10 @@ public interface Converter { // todo: introduce type parameters
*
* @param <T> – value type
*/
public static class MaxHolder { // todo: refactor class to make it generic
private Object max;
public static class MaxHolder<T extends Comparable<? super T>> { // todo: refactor class to make it generic
private T max;

public MaxHolder(Object max) {
public MaxHolder(T max) {
this.max = max;
}

Expand All @@ -82,11 +80,13 @@ public MaxHolder(Object max) {
*
* @param val a new value
*/
public void put(Object val) {
throw new ExerciseNotCompletedException(); // todo: update parameter and implement the method
public void put(T val) {
if (val.compareTo(max) > 0 ){
max = val; // todo: update parameter and implement the method
}
}

public Object getMax() {
public T getMax() {
return max;
}
}
Expand All @@ -97,8 +97,8 @@ public Object getMax() {
*
* @param <T> – the type of objects that can be processed
*/
interface StrictProcessor { // todo: make it generic
void process(Object obj);
interface StrictProcessor<T extends Serializable & Comparable <? super T>> { // todo: make it generic
void process(T obj);
}

/**
Expand All @@ -108,10 +108,10 @@ interface StrictProcessor { // todo: make it generic
* @param <T> – a type of the entity that should be a subclass of {@link BaseEntity}
* @param <C> – a type of any collection
*/
interface CollectionRepository { // todo: update interface according to the javadoc
void save(Object entity);
interface CollectionRepository<T extends BaseEntity, C extends Collection<T>> { // todo: update interface according to the javadoc
void save(T entity);

Collection<Object> getEntityCollection();
C getEntityCollection();
}

/**
Expand All @@ -120,7 +120,7 @@ interface CollectionRepository { // todo: update interface according to the java
*
* @param <T> – a type of the entity that should be a subclass of {@link BaseEntity}
*/
interface ListRepository { // todo: update interface according to the javadoc
interface ListRepository<T extends BaseEntity> extends CollectionRepository<T, List<T>> { // todo: update interface according to the javadoc
}

/**
Expand All @@ -133,7 +133,10 @@ interface ListRepository { // todo: update interface according to the javadoc
*
* @param <E> a type of collection elements
*/
interface ComparableCollection { // todo: refactor it to make generic and provide a default impl of compareTo
interface ComparableCollection<E> extends Collection<E>, Comparable<Collection<?>> {
default int compareTo(Collection<?> o){
return Integer.compare(this.size(), o.size()); // todo: refactor it to make generic and provide a default impl of compareTo
}
}

/**
Expand All @@ -147,7 +150,7 @@ static class CollectionUtil {
*
* @param list
*/
public static void print(List<Integer> list) {
public static void print(List<?> list) {
// todo: refactor it so the list of any type can be printed, not only integers
list.forEach(element -> System.out.println(" – " + element));
}
Expand All @@ -160,8 +163,9 @@ public static void print(List<Integer> list) {
* @param entities provided collection of entities
* @return true if at least one of the elements has null id
*/
public static boolean hasNewEntities(Collection<BaseEntity> entities) {
throw new ExerciseNotCompletedException(); // todo: refactor parameter and implement method
public static boolean hasNewEntities(Collection<? extends BaseEntity> entities) {
return entities.stream()
.anyMatch(e ->e.getUuid() == null) ; // todo: refactor parameter and implement method
}

/**
Expand All @@ -173,8 +177,10 @@ public static boolean hasNewEntities(Collection<BaseEntity> entities) {
* @param validationPredicate criteria for validation
* @return true if all entities fit validation criteria
*/
public static boolean isValidCollection() {
throw new ExerciseNotCompletedException(); // todo: add method parameters and implement the logic
public static boolean isValidCollection(Collection <? extends BaseEntity> entities,
Predicate <? super BaseEntity> validationPredicate) {
return entities.stream()
.allMatch(validationPredicate); // todo: add method parameters and implement the logic
}

/**
Expand All @@ -187,8 +193,10 @@ public static boolean isValidCollection() {
* @param <T> entity type
* @return true if entities list contains target entity more than once
*/
public static boolean hasDuplicates() {
throw new ExerciseNotCompletedException(); // todo: update method signature and implement it
public static <T extends BaseEntity> boolean hasDuplicates(Collection <T> entities, T targetEntity) {
return entities.stream()
.filter(e -> e.getUuid().equals(targetEntity.getUuid()))
.count() > 1;// todo: update method signature and implement it
}

/**
Expand All @@ -201,7 +209,20 @@ public static boolean hasDuplicates() {
* @return optional max value
*/
// todo: create a method and implement its logic manually without using util method from JDK

public static <T> Optional<T> findMax(Iterable<T> elements, Comparator<? super T> comparator) {
var iterator = elements.iterator();
if (!iterator.hasNext()) {
return Optional.empty();
}
var max = iterator.next();
while (iterator.hasNext()) {
var element = iterator.next();
if (comparator.compare(element, max) > 0) {
max = element;
}
}
return Optional.of(max);
}
/**
* findMostRecentlyCreatedEntity is a generic util method that accepts a collection of entities and returns the
* one that is the most recently created. If collection is empty,
Expand All @@ -215,7 +236,10 @@ public static boolean hasDuplicates() {
* @return an entity from the given collection that has the max createdOn value
*/
// todo: create a method according to JavaDoc and implement it using previous method

public static <T extends BaseEntity> T findMostRecentlyCreatedEntity(Collection<T> entities) {
return findMax(entities, CREATED_ON_COMPARATOR)
.orElseThrow();
}
/**
* An util method that allows to swap two elements of any list. It changes the list so the element with the index
* i will be located on index j, and the element with index j, will be located on the index i.
Expand All @@ -228,8 +252,12 @@ public static boolean hasDuplicates() {
public static void swap(List<?> elements, int i, int j) {
Objects.checkIndex(i, elements.size());
Objects.checkIndex(j, elements.size());
throw new ExerciseNotCompletedException(); // todo: complete method implementation
swapHelper(elements, i, j); // todo: complete method implementation
}
private static <T> void swapHelper(List<T> elements, int i, int j) {
T temp = elements.get(i);
elements.set(i, elements.get(j));
elements.set(j, temp);
}

}
}