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
11 changes: 10 additions & 1 deletion fminkin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,16 @@
<version>1.3</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.190</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package ru.fizteh.fivt.students.fminkin.collectionsql;
import ru.fizteh.fivt.students.fminkin.collectionsql.impl.aggregates.Average;
import ru.fizteh.fivt.students.fminkin.collectionsql.impl.aggregates.Count;
import ru.fizteh.fivt.students.fminkin.collectionsql.impl.aggregates.Max;
import ru.fizteh.fivt.students.fminkin.collectionsql.impl.aggregates.Min;

import java.util.function.Function;
/**
* Created by Федор on 17.12.2015.
*/

public class Aggregates {
public static <T, R extends Comparable> Function<T, R> max(Function<T, R> expression) {
return new Max<>(expression);
}

public static <C, T extends Comparable<T>> Function<C, T> min(Function<C, T> expression) {
return new Min<>(expression);
}

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 Average<>(expression);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
package ru.fizteh.fivt.students.fminkin.collectionsql;

/**
* Created by Федор on 16.12.2015.
**/

import java.lang.reflect.InvocationTargetException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

public class CollectionsQL {
public static void main(String[] args) throws InvocationTargetException, NoSuchMethodException,
InstantiationException, IllegalAccessException {
}

public static class Student {
private final String name;

private final LocalDate dateOfBirth;

private final String group;

public String getName() {
return name;
}

public Student(String newName, LocalDate newDateOfBirth, String newGroup) {
name = newName;
dateOfBirth = newDateOfBirth;
group = newGroup;
}

public Student(String newName, String newGroup) {
name = newName;
dateOfBirth = null;
group = newGroup;
}

public LocalDate getDateOfBirth() {
return dateOfBirth;
}

public String getGroup() {
return group;
}
public Double age() {
return (double) ChronoUnit.YEARS.between(getDateOfBirth(), LocalDateTime.now());
}

public static Student student(String name, LocalDate dateOfBirth, String group) {
return new Student(name, dateOfBirth, group);
}

@Override
public String toString() {
StringBuilder result = new StringBuilder().append("Student{");
if (group != null) {
result.append("group = '").append(group).append('\'');
}
if (name != null) {
result.append(", name = ").append(name);
}
if (dateOfBirth != null) {
result.append(", age = ").append(dateOfBirth);
}
result.append("}\n");
return result.toString();
}
}

public static class Group {
private final String group;
private final String mentor;

public Group(String group, String mentor) {
this.group = group;
this.mentor = mentor;
}

public String getGroup() {
return group;
}

public String getMentor() {
return mentor;
}

public static Group group(String ggroup, String mmentor) {
return new Group(ggroup, mmentor);
}

@Override
public String toString() {
StringBuilder result = new StringBuilder().append("Student{");
if (group != null) {
result.append("group='").append(group).append('\'');
}
if (mentor != null) {
result.append(", name=").append(mentor);
}
result.append("}\n");
return result.toString();
}
}


public static class Statistics {

private final String group;
private final Integer count;
private final Double age;

public String getGroup() {
return group;
}

public Integer getCount() {
return count;
}

public Double getAge() {
return age;
}

public Statistics(String group, Integer count) {
this.group = group;
this.count = count;
this.age = null;
}

public Statistics(String group, Integer count, Double age) {
this.group = group;
this.count = count;
this.age = age;
}

public Statistics(String group) {
this.group = group;
this.count = null;
this.age = null;
}

@Override
public String toString() {
StringBuilder result = new StringBuilder().append("Statistics{");
if (group != null) {
result.append("group='").append(group).append('\'');
}
if (count != null) {
result.append(", count=").append(count);
}
if (age != null) {
result.append(", age=").append(age);
}
result.append("}\n");
return result.toString();
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package ru.fizteh.fivt.students.fminkin.collectionsql;

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,17 @@
package ru.fizteh.fivt.students.fminkin.collectionsql;

import java.util.Comparator;
import java.util.function.Function;

/**
* Created by Федор on 16.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));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package ru.fizteh.fivt.students.fminkin.collectionsql;

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

/**
* Created by Федор on 16.12.2015.
*/

public class Sources {
public static <Type> List<Type> list(Type... data) {
return new ArrayList<>(Arrays.asList(data));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package ru.fizteh.fivt.students.fminkin.collectionsql.impl;

/**
* Created by Федор on 17.12.2015.
*/

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 From<T> {
private List<T> elements = new ArrayList<>();
public List<T> getElements() {
return elements;
}
public From(Iterable<T> iterable) {
for (T it : iterable) {
elements.add(it);
}
}
public static <T> From<T> from(Iterable<T> iterable) {
return new From<>(iterable);
}
@SafeVarargs //Idea advices placing it
public final <R> Select<T, R> select(Class<R> returnClass, Function<T, ?>... functions) {
return new Select<>(elements, returnClass, false, functions);
}
@SafeVarargs //Idea advices placing it
public final <R> Select<T, R> selectDistinct(Class<R> returnClass, Function<T, ?>... functions) {
return new Select<>(elements, returnClass, true, functions);
}

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

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

public class JoinClause<S, J> {
private List<S> firstPart = new ArrayList<>();
private List<J> secondPart = new ArrayList<>();
private List<Tuple<S, J>> elements = new ArrayList<>();

public JoinClause(List<S> firstPart, Iterable<J> secondPart) {
this.firstPart.addAll(firstPart.stream().collect(Collectors.toList()));
for (J curr : secondPart) {
this.secondPart.add(curr);
}
}
public From<Tuple<S, J>> on(BiPredicate<S, J> condition) {
for (S first : firstPart) {
for (J second : secondPart) {
if (condition.test(first, second)) {
elements.add(new Tuple<>(first, second));
}
}
}
return new From<>(elements);
}
public <K extends Comparable<?>> From<Tuple<S, J>> on(Function<S, K> leftKey, Function<J, K> rightKey) {
HashMap<K, List<J>> map = new HashMap<>();
for (J element : secondPart) {
K key = rightKey.apply(element);
if (!map.containsKey(key)) {
map.put(key, new ArrayList<>());
}
map.get(key).add(element);
}
for (S first : firstPart) {
K key = leftKey.apply(first);
if (map.containsKey(key)) {
List<J> second = map.get(key);
second.forEach(s -> elements.add(new Tuple<>(first, s)));
}
}
return new From<>(elements);
}
}
}
Loading