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
58 changes: 58 additions & 0 deletions evlinkov/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?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.fizteh.fivt.students</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>ru.fizteh.fivt.students</groupId>
<artifactId>evlinkov</artifactId>
<version>1.0-SNAPSHOT</version>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.twitter4j</groupId>
<artifactId>twitter4j-stream</artifactId>
<version>4.0.4</version>
</dependency>

<dependency>
<groupId>com.beust</groupId>
<artifactId>jcommander</artifactId>
<version>1.48</version>
</dependency>

<dependency>
<groupId>com.google.maps</groupId>
<artifactId>google-maps-services</artifactId>
<version>0.1.7</version>
</dependency>

<dependency>
<groupId>com.google.code.geocoder-java</groupId>
<artifactId>geocoder-java</artifactId>
<version>0.16</version>
</dependency>
<dependency>
<groupId>antlr</groupId>
<artifactId>antlr</artifactId>
<version>2.7.7</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20151123</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package ru.fizteh.fivt.students.evlinkov.threads;

import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

class BlockingQueue<T> {
private int maxQueueSize;
private Queue<T> elements;
BlockingQueue(int maxSize) {
maxQueueSize = maxSize;
elements = new LinkedList<>();
}
public synchronized void offer(List<T> e) throws InterruptedException {
while (elements.size() + e.size() > maxQueueSize) {
wait();
}
for (int i = 0; i < e.size(); ++i) {
elements.add(e.get(i));
}
notifyAll();
}
public synchronized List<T> take(int n) throws InterruptedException {
while (elements.size() < n) {
wait();
}
List<T> answer;
answer = new LinkedList<>();
for (int i = 0; i < n; ++i) {
answer.add(elements.remove());
}
notifyAll();
return answer;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package ru.fizteh.fivt.students.evlinkov.threads;

import java.util.Random;

public class ThreadMuster {
public static class Runner {
private Random randNumber = new Random();
private boolean ready = false;
private int printString = 1;
private int currentNumber = 0;
class AnswerQuestion extends Thread {
private volatile int numberOfThread;
AnswerQuestion(int number) {
numberOfThread = number + 1;
}
@Override
public void run() {
while (true) {
while (printString != numberOfThread) {
if (ready) {
return;
}
try {
Thread.sleep(100);
} catch (InterruptedException e) { }
}
int doit = randNumber.nextInt(10) + 1;
if (doit == 10) {
System.out.print("No\n");
} else {
System.out.print("Yes\n");
currentNumber++;
}
printString++;
}
}
}
void run(int n) {
System.out.print("Are you ready?\n");
AnswerQuestion[] threads = new AnswerQuestion[n];
for (int i = 0; i < n; ++i) {
threads[i] = new AnswerQuestion(i);
threads[i].start();
}
while (true) {
if (printString == n + 1) {
if (currentNumber == n) {
ready = true;
break;
} else {
System.out.print("Are you ready?\n");
currentNumber = 0;
printString = 1;
}
} else {
try {
Thread.sleep(1000);
} catch (InterruptedException e) { }
}
}
for (int i = 0; i < n; ++i) {
try {
threads[i].join();
} catch (InterruptedException e) { }
}
}
}
public static void main(String[] args) {
int n;
if (args.length > 0) {
n = Integer.parseInt(args[0]);
} else {
n = 0;
}
new Runner().run(n);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package ru.fizteh.fivt.students.evlinkov.threads;

public class ThreadRhymes {
public static class Runner {
private int currentNumber = 1;
class PutsThread extends Thread {
private volatile int numberOfThread;
PutsThread(int number) {
numberOfThread = number + 1;
}
@Override
public void run() {
while (true) {
while (currentNumber != numberOfThread) {
try {
Thread.sleep(100);
} catch (InterruptedException e) { }
}
System.out.print("Thread-" + numberOfThread + "\n");
currentNumber++;
}
}
}
void run(int n) {
PutsThread[] threads = new PutsThread[n];
for (int i = 0; i < n; ++i) {
threads[i] = new PutsThread(i);
threads[i].start();
}
while (true) {
if (currentNumber == n + 1) {
currentNumber = 1;
} else {
try {
Thread.sleep(1000);
} catch (InterruptedException e) { }
}
}
}
}
public static void main(String[] args) {
int n;
if (args.length > 0) {
n = Integer.parseInt(args[0]);
} else {
n = 0;
}
new Runner().run(n);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package ru.fizteh.fivt.students.evlinkov.twitterstream;
/**
* Created by evlinkov on 17.12.15.
*/
import com.google.maps.model.GeocodingResult;
import com.google.maps.GeoApiContext;
import com.google.maps.GeocodingApi;
import com.google.maps.model.Bounds;
import com.google.maps.model.LatLng;

import java.io.FileNotFoundException;
import java.io.FileInputStream;
import static java.lang.Math.*;
import java.util.Properties;
import java.io.IOException;
import java.io.InputStream;

public class Location {
private static final double RADIUS_OF_THE_EARTH = 6371;
private GeocodingResult result;
private double radius;
Location(String place) throws Exception {
if (!place.equals("nearby")) {
String apiKey = getKeyFromProperties();
GeoApiContext context = new GeoApiContext().setApiKey(apiKey);
result = GeocodingApi.geocode(context, place).await()[0];
radius = calculateRadius();
}
}
private String getKeyFromProperties() throws IOException {
Properties property = new Properties();
try (InputStream input = new FileInputStream("twitter4j.properties")) {
property.load(input);
} catch (FileNotFoundException e) {
System.err.println("Not found the file : " + e.getMessage());
throw e;
} catch (IOException e) {
System.err.println("Not read the file : " + e.getMessage());
throw e;
}
return property.getProperty("googleApiKey");
}
public LatLng getLocation() {
return result.geometry.location;
}
public double getRadius() {
return radius;
}
private double calculateRadius() {
LatLng pointerFirst = result.geometry.bounds.northeast;
LatLng pointerSecond = result.geometry.bounds.southwest;
double rad = 180.0 / PI;
double x = cos(pointerFirst.lat / rad) * cos(pointerFirst.lng / rad) * cos(pointerSecond.lat / rad)
* cos(pointerSecond.lng / rad);
double y = cos(pointerFirst.lat / rad) * sin(pointerFirst.lng / rad) * cos(pointerSecond.lat / rad)
* sin(pointerSecond.lng / rad);
double z = sin(pointerFirst.lat / rad) * sin(pointerSecond.lat / rad);

return RADIUS_OF_THE_EARTH * acos(x + y + z);
}
public final Bounds getBounds() {
return result.geometry.bounds;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package ru.fizteh.fivt.students.evlinkov.twitterstream;
/**
* Created by evlinkov on 17.12.15.
*/
import com.beust.jcommander.Parameter;

public class Parameters {
static final int USUAL_TWEETS_LIMIT = 555;
@Parameter(names = {"--query", "-q"},
description = "Search query")
private String query = "";
@Parameter(names = {"--place", "-p"},
description = "Search place")
private String place = "";
@Parameter(names = {"--stream", "-s"},
description = "Stream mode")
private boolean stream = false;
@Parameter(names = {"--hideRetweets"},
description = "Hide retweets")
private boolean hideRetweets = false;
@Parameter(names = {"--limit", "-l"},
description = "Limit tweets")
private int limitTweets = USUAL_TWEETS_LIMIT;
@Parameter(names = {"--help", "-h"},
description = "Help mode")
private boolean helpMode = false;
public String getQuery() {
return query;
}
public String getPlace() {
return place;
}
public boolean checkStream() {
return stream;
}
public boolean checkHideRetweets() {
return hideRetweets;
}
public int getLimitTweets() {
return limitTweets;
}
public boolean checkHelpMode() {
return helpMode;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package ru.fizteh.fivt.students.evlinkov.twitterstream;
/**
* Created by evlinkov on 17.12.15.
*/
import java.time.temporal.ChronoUnit;
import java.time.ZoneId;
import java.util.Date;
import java.time.*;

public class Time {
public static String printTime(long tweetTime, long currentTime) {
LocalDateTime currentTimer = new Date(currentTime).toInstant()
.atZone(ZoneId.systemDefault()).toLocalDateTime();
LocalDateTime tweetTimer = new Date(tweetTime).toInstant()
.atZone(ZoneId.systemDefault()).toLocalDateTime();
if (tweetTimer.isAfter(currentTimer.minusMinutes(2))) {
return "Только что";
} else {
if (tweetTimer.isAfter(currentTimer.minusHours(1))) {
return new StringBuilder().append(ChronoUnit.MINUTES.between(tweetTimer, currentTimer))
.append(" минут назад").toString();
} else {
if (currentTimer.toLocalDate().isEqual(tweetTimer.toLocalDate())) {
return new StringBuilder().append(ChronoUnit.HOURS.between(tweetTimer, currentTimer))
.append(" часов назад").toString();
} else {
if (tweetTimer.toLocalDate().isEqual(currentTimer.minusDays(1).toLocalDate())) {
return "вчера";
} else {
return new StringBuilder().append(ChronoUnit.DAYS.between(tweetTimer, currentTimer))
.append(" дней назад").toString();
}
}
}
}
}
}
Loading