You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
* {@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,9 @@ public static class Limited {
* @param <T> – source object type
* @param <R> - converted result type
*/
public interface Converter { // todo: introduce type parameters
public interface Converter <T,R> {
// todo: add convert method
R convert(T obj);
}
/**
Expand All
@@ -70,10 +69,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 +81,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;
}
}
public Object getMax() {
public T getMax() {
return max;
}
}
Expand All
@@ -97,8 +98,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 +109,9 @@ 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);
Collection<Object> getEntityCollection();
interface CollectionRepository<T extends BaseEntity, C extends Collection<T>> { // todo: update interface according to the javadoc
void save(T entity);
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,12 +133,14 @@ 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<?>> { // todo: refactor it to make generic and provide a default impl of compareTo
@Override
default int compareTo(Collection<?> o) {
return Integer.compare(this.size(), o.size());
}
}
/**
* {@link CollectionUtil} is an util class that provides various generic helper methods.
*/
static class CollectionUtil {
static final Comparator<BaseEntity> CREATED_ON_COMPARATOR = Comparator.comparing(BaseEntity::getCreatedOn);
Expand All
@@ -147,7 +149,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
throw new ExerciseNotCompletedException(); // todo: complete method implementation
swapHelper(elements, i, j);
}
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);
}
}
}
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.