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
173 changes: 173 additions & 0 deletions solutions/bradikhin/ANkoR911/CQL/Aggregates.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@


import impl.EmptyCollectionException;

import java.util.Collection;
import java.util.Collections;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
* Created by Владимир on 19.12.2015.
*/

public class Aggregates
{

public interface Agregator<C, T> extends Function<C, T>
{
T apply(Collection<C> collection) throws EmptyCollectionException;
}

/**
* Maximum value for expression for elements of given collecdtion.
*
* @param expression
* @param <C>
* @param <T>
* @return
*/
public static <C, T extends Comparable<T>> Agregator<C, T> max(Function<C, T> expression)
{
return new Agregator<C, T>()
{
@Override
public T apply(Collection<C> collection) throws EmptyCollectionException
{
if (collection.isEmpty())
{
throw new EmptyCollectionException("Method max was called for empty collection.");
}
return Collections.max(collection.stream().map(expression).collect(Collectors.toList()));
}

@Override
public T apply(C c)
{
return expression.apply(c);
}
};
}

/**
* Minimum value for expression for elements of given collecdtion.
*
* @param expression
* @param <C>
* @param <T>
* @return
*/
public static <C, T extends Comparable<T>> Agregator<C, T> min(Function<C, T> expression)
{
return new Agregator<C, T>()
{
@Override
public T apply(Collection<C> collection) throws EmptyCollectionException
{
if (collection.isEmpty())
{
throw new EmptyCollectionException("Method min was called for empty collection.");
}
return Collections.min(collection.stream().map(expression).collect(Collectors.toList()));
}

@Override
public T apply(C c)
{
return expression.apply(c);
}
};
}

/**
* Number of items in source collection that turns this expression into not null.
*
* @param expression
* @param <C>
* @param <T>
* @return
*/
public static <C, T extends Comparable<T>> Agregator<C, Long> count(Function<C, T> expression)
{
return new Agregator<C, Long>()
{
@Override
public Long apply(Collection<C> collection)
{
if (collection.isEmpty())
{
return 0L;
}
return collection.stream().map(expression).count();
}

@Override
public Long apply(C c)
{
if (expression.apply(c) != null)
{
return 1L;
}
return 0L;
}
};
}

/**
* Average value for expression for elements of given collection.
*
* @param expression
* @param <C>
* @param <T>
* @return
*/
public static <C, T extends Number> Agregator<C, T> avg(Function<C, T> expression)
{
return new Agregator<C, T>()
{
@Override
public T apply(Collection<C> collection) throws EmptyCollectionException
{
if (collection.isEmpty())
{
throw new EmptyCollectionException("Method avg was called for empty collection.");
}
T example = expression.apply(collection.iterator().next());
if (example instanceof Integer)
{
int sum = 0;
for (C elem : collection)
{
sum += (Integer) expression.apply(elem);
}
return (T) Integer.valueOf(sum / collection.size());
}
if (example instanceof Long)
{
int sum = 0;
for (C elem : collection)
{
sum += (Long) expression.apply(elem);
}
return (T) Long.valueOf(sum / collection.size());
}
if (example instanceof Double || example instanceof Float)
{
double sum = 0;
for (C elem : collection)
{
sum += (Long) expression.apply(elem);
}
return (T) Double.valueOf(sum / collection.size());
}
throw new ClassCastException("Class type is not detected!");
}

@Override
public T apply(C c)
{
return expression.apply(c);
}
};
}
}
18 changes: 18 additions & 0 deletions solutions/bradikhin/ANkoR911/CQL/Conditions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import java.util.function.Function;
import java.util.function.Predicate;

/**
* Created by Владимир on 19.12.2015.
*/
public class Conditions<T>
{
public static <T> Predicate<T> rlike(Function<T, String> expression, String regexp)
{
return element -> expression.apply(element).matches(regexp);
}

public static <T> Predicate<T> like(Function<T, String> expression, String pattern)
{
throw new UnsupportedOperationException();
}
}
20 changes: 20 additions & 0 deletions solutions/bradikhin/ANkoR911/CQL/OrderByConditions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import java.util.Comparator;
import java.util.function.Function;

/**
* Created by Владимир on 19.12.2015.
*/


public class OrderByConditions
{
public static <T, R extends Comparable<R>> Comparator<T> asc(Function<T, R> expression)
{
return (o1, o2) -> expression.apply(o1).compareTo(expression.apply(o2));
}

public static <T, R extends Comparable<R>> Comparator<T> desc(Function<T, R> expression)
{
return (o1, o2) -> expression.apply(o2).compareTo(expression.apply(o1));
}
}
70 changes: 70 additions & 0 deletions solutions/bradikhin/ANkoR911/CQL/Sources.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.util.*;
import java.util.stream.Stream;

/**
* Created by Владимир on 19.12.2015.
*/

public class Sources
{

/**
* @param items
* @param <T>
* @return
*/
@SafeVarargs
public static <T> List<T> list(T... items) {
return Arrays.asList(items);
}

/**
* @param items
* @param <T>
* @return
*/
@SafeVarargs
public static <T> Set<T> set(T... items) {
return new HashSet<>(Arrays.asList(items));
}

/**
* @param inputStream
* @param <T>
* @return
* @throws IOException
* @throws ClassCastException
*/
public static <T> Stream<T> lines(InputStream inputStream) throws IOException, ClassCastException {
final int bufferSize = 1000;
StringBuilder builder = new StringBuilder();
byte[] data = new byte[bufferSize];
Stream.Builder<T> stream = Stream.builder();

while (inputStream.read(data) > 0) {
builder.append(data);
}
String[] lines = builder.toString().split("[\n]");
//предполагается что каждый элемент на отдельной строке
for (String line : lines) {
stream.add((T) line);
}
return stream.build();
}

/**
* @param file
* @param <T>
* @return
* @throws IOException
* @throws ClassCastException
* @throws UnsupportedOperationException if this {@code Path} is not associated with the default provider
*/
public static <T> Stream<T> lines(Path file) throws IOException, ClassCastException {
return lines(new FileInputStream(file.toFile()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Created by Владимир on 19.12.2015.
*/
public class EmptyCollectionException extends Exception
{
public EmptyCollectionException(String message)
{
super(message);
}
}
Loading