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
18 changes: 12 additions & 6 deletions okalitova/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@
<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>

<dependency>
<groupId>org.twitter4j</groupId>
Expand Down Expand Up @@ -53,5 +47,17 @@
<version>0.4</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.190</version>
</dependency>

</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package ru.fizteh.fivt.students.okalitova.collectionsql;

import ru.fizteh.fivt.students.okalitova.collectionsql.Aggregators.Avg;
import ru.fizteh.fivt.students.okalitova.collectionsql.Aggregators.Count;
import ru.fizteh.fivt.students.okalitova.collectionsql.Aggregators.Max;
import ru.fizteh.fivt.students.okalitova.collectionsql.Aggregators.Min;

import java.util.function.Function;

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, Long> count(Function<T, ?> expression) {
return new Count<>(expression);
}

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

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

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

/**
* Created by nimloth on 16.12.15.
*/
public interface Aggregator<T, R> extends Function<T, R> {
R apply(List<T> elements);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package ru.fizteh.fivt.students.okalitova.collectionsql.Aggregators;

/**
* Created by nimloth on 17.12.15.
*/
import java.util.List;
import java.util.function.Function;

public 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> elements) {
return elements
.stream()
.map(function)
.mapToDouble(element -> (Double) element)
.average()
.getAsDouble();
}

@Override
public Double apply(T t) {
return null;
}
}



Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package ru.fizteh.fivt.students.okalitova.collectionsql.Aggregators;

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

/**
* Created by nimloth on 17.12.15.
*/
public class Count<T> implements Aggregator<T, Long> {

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

@Override
public Long apply(List<T> elements) {
Long longAns = elements
.stream()
.map(function)
.distinct()
.count();
return longAns;
}

@Override
public Long apply(T t) {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package ru.fizteh.fivt.students.okalitova.collectionsql.Aggregators;

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

/**
* Created by nimloth on 16.12.15.
*/
public class Max<T, R extends Comparable<R>> implements Aggregator<T, R> {

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

@Override
public R apply(List<T> elements) {
return elements
.stream()
.map(function)
.max(R::compareTo)
.get();
}

@Override
public R apply(T t) {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package ru.fizteh.fivt.students.okalitova.collectionsql.Aggregators;

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

/**
* Created by nimloth on 17.12.15.
*/
public class Min<T, R extends Comparable<R>> implements Aggregator<T, R> {

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

@Override
public R apply(List<T> elements) {
return elements
.stream()
.map(function)
.min(R::compareTo)
.get();
}

@Override
public R apply(T t) {
return null;
}
}
Loading