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

Анна Власова, 496: Threads + CQL + CQLex + TwitterStream + ModuleTests #72

Open
wants to merge 9 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
1 change: 1 addition & 0 deletions projects/annnvl/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
twitter4j.properties
59 changes: 59 additions & 0 deletions projects/annnvl/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?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>annnvl</artifactId>
<version>1.0-SNAPSHOT</version>
<name>annnvl</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>4.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.twitter4j</groupId>
<artifactId>twitter4j-stream</artifactId>
<version>4.0.4</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-junit</artifactId>
<version>2.0.0.0</version>
</dependency>

<dependency>
<groupId>com.google.code.geocoder-java</groupId>
<artifactId>geocoder-java</artifactId>
<version>0.16</version>
</dependency>
<dependency>
<groupId>com.google.maps</groupId>
<artifactId>google-maps-services</artifactId>
<version>0.1.7</version>
</dependency>
<dependency>
<groupId>com.beust</groupId>
<artifactId>jcommander</artifactId>
<version>1.48</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package ru.mipt.diht.students.annnvl.CQL;
/**
* Aggregate functions.
*/
import java.util.function.Function;
import ru.mipt.diht.students.annnvl.CQL.impl.Avg;
import ru.mipt.diht.students.annnvl.CQL.impl.Count;
import ru.mipt.diht.students.annnvl.CQL.impl.Max;
import ru.mipt.diht.students.annnvl.CQL.impl.Min;

public class Aggregates {

/**
* Maximum value for expression for elements of given collecdtion.
*
* @param expression
* @param <C>
* @param <T>
* @return
*/
public static <C, T extends Comparable<T>> Function<C, T> max(Function<C, T> expression) {
return new Max<>(expression);
}

/**
* Minimum value for expression for elements of given collecdtion.
*
* @param expression
* @param <C>
* @param <T>
* @return
*/
public static <C, T extends Comparable<T>> Function<C, T> min(Function<C, T> expression) {
return new Min<>(expression);
}

/**
* Number of items in source collection that turns this expression into not null.
*
* @param expression
* @param <T>
* @return
*/
public static <T> Function<T, Integer> count(Function<T, ?> expression) {
return new Count<>(expression);
}


/**
* Average value for expression for elements of given collection.
*
* @param expression
* @param <T>
* @return
*/
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,107 @@
package ru.mipt.diht.students.annnvl.CQL;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

import static ru.mipt.diht.students.annnvl.CQL.Aggregates.avg;
import static ru.mipt.diht.students.annnvl.CQL.Aggregates.count;
import static ru.mipt.diht.students.annnvl.CQL.CollectionQuery.Student.student;
import static ru.mipt.diht.students.annnvl.CQL.Conditions.rlike;
import static ru.mipt.diht.students.annnvl.CQL.OrderByConditions.asc;
import static ru.mipt.diht.students.annnvl.CQL.OrderByConditions.desc;
import static ru.mipt.diht.students.annnvl.CQL.Sources.list;
import static ru.mipt.diht.students.annnvl.CQL.impl.FromStmt.from;

public class CollectionQuery {

public static void main(String[] args) {
Iterable<Statistics> statistics =
from(list(
student("ivanov", LocalDate.parse("1986-08-06"), "494"),
student("ivanov", LocalDate.parse("1986-08-06"), "494")))
.select(Statistics.class, Student::getGroup, count(Student::getGroup), avg(Student::age))
.where(rlike(Student::getName, ".*ov").and(s -> s.age() > 20))
.groupBy(Student::getGroup)
.having(s -> s.getCount() > 0)
.orderBy(asc(Student::getGroup), desc(count(Student::getGroup)))
.limit(100)
.union()
.from(list(student("ivanov", LocalDate.parse("1985-08-06"), "494")))
.selectDistinct(Statistics.class, s -> "all", count(s -> 1), avg(Student::age))
.execute();
System.out.println(statistics);
}


public static class Student {
private final String name;

private final LocalDate dateOfBith;

private final String group;

public String getName() {
return name;
}

public Student(String name, LocalDate dateOfBith, String group) {
this.name = name;
this.dateOfBith = dateOfBith;
this.group = group;
}

public LocalDate getDateOfBith() {
return dateOfBith;
}

public String getGroup() {
return group;
}

public long age() {
return ChronoUnit.YEARS.between(getDateOfBith(), LocalDateTime.now());
}

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


public static class Statistics {

private final String group;
private final Long count;
private final Long age;

public String getGroup() {
return group;
}

public Long getCount() {
return count;
}


public Long getAge() {
return age;
}

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

@Override
public String toString() {
return "Statistics{"
+ "group='" + group + '\''
+ ", count=" + count
+ ", age=" + age
+ '}';
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package ru.mipt.diht.students.annnvl.CQL;

import java.util.function.Function;
import java.util.function.Predicate;

/**
* Where clause conditions.
*/
public class Conditions<T> {

/**
* Matches string result of expression against regexp pattern.
*
* @param expression expression result to match
* @param regexp pattern to match to
* @param <T> source object type
* @return
*/
public static <T> Predicate<T> rlike(Function<T, String> expression, String regexp) {
return element -> expression.apply(element).matches(regexp);
}

/**
* Matches string result of expression against SQL like pattern.
*
* @param expression expression result to match
* @param pattern pattern to match to
* @param <T> source object type
* @return
*/
public static <T> Predicate<T> like(Function<T, String> expression, String pattern) {
String newpattern = pattern.toLowerCase();
newpattern = newpattern.replace(".", "\\.").replace("?", ".").replace("%", ".*");
return rlike(expression, newpattern);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package ru.mipt.diht.students.annnvl.CQL;

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

/**
* OrderBy sort order helper methods.
*/
public class OrderByConditions {

/**
* Ascending comparator.
*
* @param expression
* @param <T>
* @param <R>
* @return
*/
public static <T, R extends Comparable<R>> Comparator<T> asc(Function<T, R> expression) {
return (o1, o2) -> expression.apply(o1).compareTo(expression.apply(o2));
}

/**
* Descending comparator.
*
* @param expression
* @param <T>
* @param <R>
* @return
*/
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,54 @@
package ru.mipt.diht.students.annnvl.CQL;

import java.io.InputStream;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Stream;

/**
* Helper methods to create collections.
*/
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) {
throw new UnsupportedOperationException();
}

/**
* @param inputStream
* @param <T>
* @return
*/
public static <T> Stream<T> lines(InputStream inputStream) {
throw new UnsupportedOperationException();
}

/**
* @param file
* @param <T>
* @return
*/
public static <T> Stream<T> lines(Path file) {
throw new UnsupportedOperationException();
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package ru.mipt.diht.students.annnvl.CQL.impl;

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

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,27 @@
package ru.mipt.diht.students.annnvl.CQL.impl;

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