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, Reverse #94

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
26 changes: 26 additions & 0 deletions projects/JenkaEff/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>ru.mipt.diht.students</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>ru.mipt.diht.students</groupId>
<artifactId>JenkaEff</artifactId>
<version>1.0-SNAPSHOT</version>
<name>JenkaEff</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package ru.fizteh.fivt.students.JenkaEff.CollectionQuery;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Function;

public class Aggregates {
public static <T> Function<T, Integer> count(Function<T, ?> expression) {
return new Count<>(expression);
}

public static <T> Function<T, Double> avg(Function<T, ? extends Number> expression) {
return new Avg<>(expression);
}

public interface Aggregator<T, C> extends Function<T, C> {
C apply(List<T> list);
}

public static class Count<T> implements Aggregator<T, Integer> {

private Function<T, ?> function;
public Count(Function<T, ?> expression) {
this.function = expression;
}

@Override
public Integer apply(List<T> list) {
Set<Object> distinctedList = new HashSet<>();
list.stream().filter(e -> !distinctedList.contains(function.apply(e))).forEach(e -> {
distinctedList.add(function.apply(e));
});
return distinctedList.size();
}
@Override
public Integer apply(T t) {
return null;
}
}

public static class Avg<T> implements Aggregator<T, Double> {
private Function<T, ? extends Number> function;
public Avg(Function<T, ? extends Number> expression) {
this.function = expression;
}

@Override
public Double apply(List<T> list) {
Double result = 0.0;
for (T e : list) {
result += function.apply(e).floatValue();
}
return result / list.size();
}

@Override
public Double apply(T t) {
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package ru.fizteh.fivt.students.JenkaEff.CollectionQuery;

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 e -> expression.apply(e).matches(regexp);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package ru.fizteh.fivt.students.JenkaEff.CollectionQuery;

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 (e1, e2) -> expression.apply(e1).compareTo(expression.apply(e2));
}

public static <T, R extends Comparable<R>> Comparator<T> desc(Function<T, R> expression) {
return (e1, e2) -> expression.apply(e2).compareTo(expression.apply(e1));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package ru.fizteh.fivt.students.JenkaEff.CollectionQuery;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Sources {
@SafeVarargs
public static <T> List<T> list(T... items) {
return new ArrayList<>(Arrays.asList(items));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package ru.fizteh.fivt.students.JenkaEff.CollectionQuery.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.function.BiPredicate;
import java.util.function.Function;
import java.util.stream.Collectors;

public class FromStmt<T> {
private List<T> list = new ArrayList<T>();

public FromStmt(Iterable<T> iterable) {
for (T curr : iterable) {
list.add(curr);
}
}

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

@SafeVarargs
public final <R> SelectStmt<T, R> select(Class<R> clazz, Function<T, ?>... functions) {
return new SelectStmt<>(list, clazz, false, functions);
}

@SafeVarargs
public final <R> SelectStmt<T, R> selectDistinct(Class<R> clazz, Function<T, ?>... functions) {
return new SelectStmt<>(list, clazz, true, functions);
}

public final <F, S> SelectStmt<T, Tuple<F, S>> select(Function<T, F> first, Function<T, S> second) {
return new SelectStmt<>(list, false, first, second);
}

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

public class JoinClause<S, J> {
private List<S> firstList = new ArrayList<>();
private List<J> secondList = new ArrayList<>();
private List<Tuple<S, J>> list = new ArrayList<>();

public JoinClause(List<S> firstList, Iterable<J> secondList) {
this.firstList.addAll(firstList.stream().collect(Collectors.toList()));
for (J curr : secondList) {
this.secondList.add(curr);
}
}

public FromStmt<Tuple<S, J>> on(BiPredicate<S, J> condition) {
for (S first : firstList) {
list.addAll(secondList.stream().filter(second -> condition.test(first, second))
.map(second -> new Tuple<>(first, second)).collect(Collectors.toList()));
}
return new FromStmt<>(list);
}

public <K extends Comparable<?>> FromStmt<Tuple<S, J>> on(
Function<S, K> leftKey,
Function<J, K> rightKey) {
HashMap<K, List<J>> map = new HashMap<>();
for (J e : secondList) {
K key = rightKey.apply(e);
if (!map.containsKey(key)) {
map.put(key, new ArrayList<>());
}
map.get(key).add(e);
}
for (S first : firstList) {
K key = leftKey.apply(first);
if (map.containsKey(key)) {
List<J> second = map.get(key);
second.forEach(s -> list.add(new Tuple<>(first, s)));
}
}
return new FromStmt<>(list);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package ru.fizteh.fivt.students.JenkaEff.CollectionQuery.impl;

public class Pair <T, R>{
private T key;
private R value;
Pair(T k, R v) {
key = k;
value = v;
}
public T getKey() {
return key;
}
public void setKey(T key) {
this.key = key;
}
public R getValue() {
return value;
}
public void setValue(R value) {
this.value = value;
}

}
Loading