Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Кирилл Дроздов, 497, Reverser #86

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
@@ -0,0 +1,109 @@
import ru.mipt.diht.students.drozdovkir.CQL.EmptyCollectionException;

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

public class Aggregates {

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

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);
}
};
}


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);
}
};
}


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;
}
};
}


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);
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import java.util.function.Function;
import java.util.function.Predicate;

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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public class EmptyCollectionException extends Exception {
public EmptyCollectionException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
import java.util.*;
import java.util.function.BiPredicate;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class FromStmt<T> {
private Iterable<?> previousQuery;
private Iterable<T> data;

FromStmt(Iterable<T> iterable) {
this.data = iterable;
}

FromStmt(Iterable<T> iterable, Iterable<?> previous) {
data = iterable;
previousQuery = previous;
}

private FromStmt(Stream<T> stream) {
data = stream.collect(Collectors.toList());
}

public static <T> FromStmt<T> from(Iterable<T> iterable) {
return new FromStmt<>(iterable);
}

public static <T> FromStmt<T> from(Stream<T> stream) {
return new FromStmt<>(stream);
}

public static <T> FromStmt<T> from(Query<T> query) throws QueryExecuteException, EmptyCollectionException {
return new FromStmt<>(query.execute());
}

@SafeVarargs
public final <R> SelectStmt<T, R> select(Class<R> clazz, Function<T, ?>... s) throws UnequalUnionClassesException {
try {
return new SelectStmt<>((Iterable<R>) previousQuery, data, clazz, false, s);
} catch (ClassCastException ex) {
try {
List<R> newIterable = new ArrayList<>();
previousQuery.forEach(el -> newIterable.add((R) el));
return new SelectStmt<>(newIterable, data, clazz, false, s);
} catch (ClassCastException exeption) {
throw new UnequalUnionClassesException("Uncasted classes can't be union.", ex);
}
}
}

public final <R> SelectStmt<T, R> select(Function<T, R> s) throws UnequalUnionClassesException {
try {
return new SelectStmt<>((Iterable<R>) previousQuery, data, false, s);
} catch (ClassCastException ex) {
try {
List<R> newIterable = new ArrayList<>();
previousQuery.forEach(el -> newIterable.add((R) el));
return new SelectStmt<>(newIterable, data, false, s);
} catch (ClassCastException exception) {
throw new UnequalUnionClassesException("Uncasted classes can't be union.", ex);
}
}
}

public final <F, S> SelectStmt<T, Tuple<F, S>> select(Function<T, F> first, Function<T, S> second)
throws UnequalUnionClassesException {
try {
return new SelectStmt<>((Iterable<Tuple<F, S>>) previousQuery, data, false, first, second);
} catch (ClassCastException ex) {
try {
List<Tuple<F, S>> newIterable = new ArrayList<>();
previousQuery.forEach(el -> newIterable.add((Tuple<F, S>) el));
return new SelectStmt<>(newIterable, data, false, first, second);
} catch (ClassCastException exception) {
throw new UnequalUnionClassesException("Uncasted classes can't be union.", ex);
}
}
}

@SafeVarargs
public final <R> SelectStmt<T, R> selectDistinct(Class<R> clazz, Function<T, ?>... s)
throws UnequalUnionClassesException {
try {
return new SelectStmt<>((Iterable<R>) previousQuery, data, clazz, true, s);
} catch (ClassCastException ex) {
try {
List<R> newIterable = new ArrayList<>();
previousQuery.forEach(el -> newIterable.add((R) el));
return new SelectStmt<>(newIterable, data, clazz, false, s);
} catch (ClassCastException exception) {
throw new UnequalUnionClassesException("Uncasted classes can't be union.", ex);
}
}
}


public final <R> SelectStmt<T, R> selectDistinct(Function<T, R> s) throws UnequalUnionClassesException {
try {
return new SelectStmt<>((Iterable<R>) previousQuery, data, false, s);
} catch (ClassCastException ex) {
try {
List<R> newIterable = new ArrayList<>();
previousQuery.forEach(el -> newIterable.add((R) el));
return new SelectStmt<>(newIterable, data, false, s);
} catch (ClassCastException exception) {
throw new UnequalUnionClassesException("Uncasted classes can't be union.", ex);
}
}
}

public final <F, S> SelectStmt<T, Tuple<F, S>> selectDistinct(Function<T, F> first, Function<T, S> second)
throws UnequalUnionClassesException {
try {
return new SelectStmt<>((Iterable<Tuple<F, S>>) previousQuery, data, false, first, second);
} catch (ClassCastException ex) {
try {
List<Tuple<F, S>> newIterable = new ArrayList<>();
previousQuery.forEach(el -> newIterable.add((Tuple<F, S>) el));
return new SelectStmt<>(newIterable, data, false, first, second);
} catch (ClassCastException exception) {
throw new UnequalUnionClassesException("Uncasted classes can't be union.", ex);
}
}
}

public final <J> JoinClause<T, J> join(Iterable<J> iterable) {
return new JoinClause<>(data, iterable);
}

public final <J> JoinClause<T, J> join(Stream<J> stream) {
return new JoinClause<>(data, stream.collect(Collectors.toList()));
}

public final <J> JoinClause<T, J> join(Query<J> stream) throws QueryExecuteException, EmptyCollectionException {
return new JoinClause<>(data, stream.execute());
}

public class JoinClause<T, J> {
private Iterable<T> first;
private Iterable<J> second;

JoinClause(Iterable<T> fst, Iterable<J> snd) {
this.first = fst;
this.second = snd;
}

public final FromStmt<Tuple<T, J>> on(BiPredicate<T, J> condition) {
List<T> list = new LinkedList<>();
first.forEach(list::add);

List<Tuple<T, J>> result = new ArrayList<>();
second.forEach(element -> {
for (int i = 0; i < list.size(); i++) {
if (condition.test(list.get(i), element)) {
result.add(new Tuple<>(list.get(i), element));
list.remove(i);
break;
}
}
});

return new FromStmt<>(result);
}

public final <K extends Comparable<K>> FromStmt<Tuple<T, J>> on(
Function<T, K> leftKey,
Function<J, K> rightKey) {

Map<K, List<T>> mapOfT = new HashMap<>();
first.forEach(elem -> {
K key = leftKey.apply(elem);
if (!mapOfT.containsKey(key)) {
List<T> inserted = new LinkedList<>();
inserted.add(elem);
mapOfT.put(key, inserted);
} else {
List<T> inserted = mapOfT.get(key);
inserted.add(elem);
mapOfT.put(key, inserted);
}
});

List<Tuple<T, J>> result = new ArrayList<>();
second.forEach((elem) -> {
K key = rightKey.apply(elem);
if (mapOfT.containsKey(key)) {
List<T> replaced = mapOfT.get(key);
result.add(new Tuple<>(replaced.get(0), elem));
replaced.remove(0);
if (replaced.isEmpty()) {
mapOfT.remove(key);
} else {
mapOfT.put(key, replaced);
}
}
});

return new FromStmt<>(result);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import java.util.Comparator;
import java.util.function.Function;

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));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import java.util.stream.Stream;

public interface Query<R> {
Iterable<R> execute() throws QueryExecuteException, EmptyCollectionException;
Stream<R> stream() throws QueryExecuteException, EmptyCollectionException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public class QueryExecuteException extends Exception {
QueryExecuteException(String message, Throwable reason) {
super(message, reason);
}
}
Loading