diff --git a/lib/twitter4j-core-4.0.4.jar b/lib/twitter4j-core-4.0.4.jar deleted file mode 100644 index 029abce..0000000 Binary files a/lib/twitter4j-core-4.0.4.jar and /dev/null differ diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..54d05dc --- /dev/null +++ b/pom.xml @@ -0,0 +1,106 @@ + + + 4.0.0 + + com.github.klemek + primedate + 1.0 + PrimeDate + jar + + + 3.0.0 + + + + UTF-8 + UTF-8 + + + + + org.twitter4j + twitter4j-core + 4.0.7 + + + + junit + junit + 4.12 + test + + + + + ${project.build.directory}/classes + src/main/java + src/test/java + + + src/main/resources + + **/*.java + + + + + + src/test/resources + + **/*.java + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.7.0 + + 1.8 + 1.8 + + + + org.apache.maven.plugins + maven-dependency-plugin + + + copy-dependencies + prepare-package + + unpack-dependencies + + + ${project.build.directory}/classes + runtime + + + + + + + org.apache.maven.plugins + maven-jar-plugin + 3.1.0 + + + + fr.klemek.englishparser.Launch + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.22.0 + + + + \ No newline at end of file diff --git a/src/META-INF/MANIFEST.MF b/src/META-INF/MANIFEST.MF deleted file mode 100644 index d9d86b1..0000000 --- a/src/META-INF/MANIFEST.MF +++ /dev/null @@ -1,3 +0,0 @@ -Manifest-Version: 1.0 -Main-Class: fr.klemek.primedate.MainProcess - diff --git a/src/fr/klemek/primedate/MainProcess.java b/src/main/java/fr/klemek/primedate/MainProcess.java similarity index 97% rename from src/fr/klemek/primedate/MainProcess.java rename to src/main/java/fr/klemek/primedate/MainProcess.java index 1b2767f..c35cd97 100644 --- a/src/fr/klemek/primedate/MainProcess.java +++ b/src/main/java/fr/klemek/primedate/MainProcess.java @@ -1,157 +1,157 @@ -package fr.klemek.primedate; - -import java.text.NumberFormat; -import java.text.SimpleDateFormat; -import java.util.Calendar; -import java.util.concurrent.ThreadLocalRandom; -import java.util.Locale; -import java.util.Timer; -import java.util.TimerTask; - -/** - * Main process to be launched - * - * @author Kleme - */ -public abstract class MainProcess { - - private final static String VERSION = "v1.5"; - - private final static SimpleDateFormat DATE_TO_NUM = new SimpleDateFormat("yyyyMMddHHmm"); - - private final static String[] GREETINGS_SENTENCES = new String[] { "Hi", "Hello there", "Good %s", "What's up?", - "Greetings", "How are you?", "Hey", "How are you doing?", "How's life?", "Long time no see", - "It's been a while", "How do you do?", "Yo", "Howdy", "Sup?", "Whazzup?", "Yoooooo", "Bonjour", "Hola", - "Hallo", "Salam", "Aloha", "Hey", "Heyy", "Heyy", "Heyyy", "Heyyyy", "Heyyyyy" }; - - private final static String[] DATE_SENTENCES = new String[] { "The date is %1$s and it's %2$s", - "We are %1$s and it's already %2$s", "It's already %2$s today", "Today is %1$s and it's %2$s", - "It's %2$s and today's %1$s", "It's %2$s on %1$s", "Already %2$s on %1$s" }; - - private final static String[] PRIME_SENTENCES = new String[] { "%s is a prime number", - "%s cannot be divided by another number", "nobody can divide %s", "%s is prime", "%s is prime as fuck", - "%s is prime as hell", "%s is prime as shit", "%s sure is prime", "%s is pretty prime", - "you can trust %s to be prime", "%s is like other primes" }; - - private final static String[] END_SENTENCES = new String[] { "", "Pretty cool, huh?", "It blows your mind!", - "You can forget it now.", "You wasted 20 seconds of your time.", "That's cool!", "Isn't it cool?", - "You should stop reading these tweets...", "That's a good password.", "Maybe not.", "Why ? ...", - "Pls help stuck in prime factory", "Why do you read these ?", "Can someone check ?", "Really ?", "Cool.", - "You can use it.", "Google it.", "How do I stop this?", "Send STOP to not receive this anymore.", "Yes.", - "It's true.", "Move on.", "That's no FAKE news!", "Soooo hard to calculate by hand!", "It is also my IQ.", - "Add 1 and it doesn't work anymore.", "I'm a bot, I know that stuff.", "Awesome!", - "Next one should be better!", "Let's calculate its factorial now...", "There's a phone number in it.", - "Don't do this at home, kids.", "Highest one so far!", "Next one will blow your mind!", - "That's the exact number of bacteria in your body.", "Don't believe me? Fine.", - "Funniest thing of the day.", "See you next time!", "Bye!", "See ya!", "See you later!" }; - - private final static char[] SENTENCE_ENDS = new char[] { '.', '!', ',' }; - - private static void checkTime(Calendar currentTime, boolean fake) { - try { - long currentTimeValue = Long.parseLong(DATE_TO_NUM.format(currentTime.getTime())); - - if (PrimeCalculator.isPrime(currentTimeValue)) { - String msg; - do { - msg = constructSentence(currentTime, currentTimeValue); - } while (msg.length() > 280); - if (fake) - System.out.println(msg); - else - TwitterClient.tweet(msg); - } - } catch (Exception e) { - e.printStackTrace(); - } - } - - private static String constructSentence(Calendar time, long timeValue) { - - final int hour = time.get(Calendar.HOUR_OF_DAY); - final String dayPeriod = hour >= 5 && hour <= 12 ? "morning" : (hour <= 18 ? "afternoon" : "evening"); - - final PrettyDate time2 = new PrettyDate(time); - - final int r1 = ThreadLocalRandom.current().nextInt(0, GREETINGS_SENTENCES.length); - final int r2 = ThreadLocalRandom.current().nextInt(0, SENTENCE_ENDS.length); - final int r3 = ThreadLocalRandom.current().nextInt(0, DATE_SENTENCES.length); - final int r4 = ThreadLocalRandom.current().nextInt(0, SENTENCE_ENDS.length); - final int r5 = ThreadLocalRandom.current().nextInt(0, PRIME_SENTENCES.length); - final int r6 = ThreadLocalRandom.current().nextInt(0, SENTENCE_ENDS.length); - final int r7 = ThreadLocalRandom.current().nextInt(0, END_SENTENCES.length); - - // GREETINGS - - String greetings = String.format(GREETINGS_SENTENCES[r1], dayPeriod); - if (!greetings.endsWith("?")) - greetings += SENTENCE_ENDS[r2]; - - // DATE - - String date = String.format(DATE_SENTENCES[r3], time2.getPrettyDate(), time2.getPrettyTime()); - if (greetings.endsWith(",")) - date = date.substring(0, 1).toLowerCase() + date.substring(1); - date += SENTENCE_ENDS[r4]; - if (date.endsWith(",")) - date = date.substring(0, date.length() - 1) + " and"; - - // PRIME - - String prime = String.format(PRIME_SENTENCES[r5], NumberFormat.getNumberInstance().format(timeValue)); - if (date.endsWith("d")) - prime = prime.substring(0, 1).toLowerCase() + prime.substring(1); - - if (END_SENTENCES[r7].length() == 0 && SENTENCE_ENDS[r6] == ',') - prime += "."; - else - prime += SENTENCE_ENDS[r6]; - - // END - - String end = END_SENTENCES[r7]; - if (prime.endsWith(",")) - end = end.substring(0, 1).toLowerCase() + end.substring(1); - - return greetings + " " + date + " " + prime + " " + end; - } - - public static void main(String[] args) { - - Locale.setDefault(Locale.ENGLISH); - - final int max_random = GREETINGS_SENTENCES.length * DATE_SENTENCES.length * PRIME_SENTENCES.length - * END_SENTENCES.length; - - System.out.println(String.format("PrimeDate %s%n%s%n%s+ sentences available randomly", VERSION, - Calendar.getInstance().getTime(), NumberFormat.getInstance().format(max_random))); - - if (args.length < 1) { - System.out.println("Argument 1 must be a file containing customer keys"); - System.exit(0); - } - - if (TwitterClient.setUpTwitter(args[0])) { - - PrimeCalculator.computeList(); - - Timer timer = new Timer(); - timer.scheduleAtFixedRate(new TimerTask() { - - @Override - public void run() { - Calendar currentTime = Calendar.getInstance(); - currentTime.add(Calendar.MILLISECOND, - -currentTime.getTimeZone().getOffset(currentTime.getTimeInMillis())); - checkTime(currentTime, false); - } - }, 0, 1 * 60 * 1000); - - /* - * Calendar time = Calendar.getInstance(); while (true) { checkTime(time, true); - * time.add(Calendar.MINUTE, 1); } - */ - } - } - -} +package fr.klemek.primedate; + +import java.text.NumberFormat; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.concurrent.ThreadLocalRandom; +import java.util.Locale; +import java.util.Timer; +import java.util.TimerTask; + +/** + * Main process to be launched + * + * @author Kleme + */ +public abstract class MainProcess { + + private final static String VERSION = "v1.5"; + + private final static SimpleDateFormat DATE_TO_NUM = new SimpleDateFormat("yyyyMMddHHmm"); + + private final static String[] GREETINGS_SENTENCES = new String[] { "Hi", "Hello there", "Good %s", "What's up?", + "Greetings", "How are you?", "Hey", "How are you doing?", "How's life?", "Long time no see", + "It's been a while", "How do you do?", "Yo", "Howdy", "Sup?", "Whazzup?", "Yoooooo", "Bonjour", "Hola", + "Hallo", "Salam", "Aloha", "Hey", "Heyy", "Heyy", "Heyyy", "Heyyyy", "Heyyyyy" }; + + private final static String[] DATE_SENTENCES = new String[] { "The date is %1$s and it's %2$s", + "We are %1$s and it's already %2$s", "It's already %2$s today", "Today is %1$s and it's %2$s", + "It's %2$s and today's %1$s", "It's %2$s on %1$s", "Already %2$s on %1$s" }; + + private final static String[] PRIME_SENTENCES = new String[] { "%s is a prime number", + "%s cannot be divided by another number", "nobody can divide %s", "%s is prime", "%s is prime as fuck", + "%s is prime as hell", "%s is prime as shit", "%s sure is prime", "%s is pretty prime", + "you can trust %s to be prime", "%s is like other primes" }; + + private final static String[] END_SENTENCES = new String[] { "", "Pretty cool, huh?", "It blows your mind!", + "You can forget it now.", "You wasted 20 seconds of your time.", "That's cool!", "Isn't it cool?", + "You should stop reading these tweets...", "That's a good password.", "Maybe not.", "Why ? ...", + "Pls help stuck in prime factory", "Why do you read these ?", "Can someone check ?", "Really ?", "Cool.", + "You can use it.", "Google it.", "How do I stop this?", "Send STOP to not receive this anymore.", "Yes.", + "It's true.", "Move on.", "That's no FAKE news!", "Soooo hard to calculate by hand!", "It is also my IQ.", + "Add 1 and it doesn't work anymore.", "I'm a bot, I know that stuff.", "Awesome!", + "Next one should be better!", "Let's calculate its factorial now...", "There's a phone number in it.", + "Don't do this at home, kids.", "Highest one so far!", "Next one will blow your mind!", + "That's the exact number of bacteria in your body.", "Don't believe me? Fine.", + "Funniest thing of the day.", "See you next time!", "Bye!", "See ya!", "See you later!" }; + + private final static char[] SENTENCE_ENDS = new char[] { '.', '!', ',' }; + + private static void checkTime(Calendar currentTime, boolean fake) { + try { + long currentTimeValue = Long.parseLong(DATE_TO_NUM.format(currentTime.getTime())); + + if (PrimeCalculator.isPrime(currentTimeValue)) { + String msg; + do { + msg = constructSentence(currentTime, currentTimeValue); + } while (msg.length() > 280); + if (fake) + System.out.println(msg); + else + TwitterClient.tweet(msg); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + private static String constructSentence(Calendar time, long timeValue) { + + final int hour = time.get(Calendar.HOUR_OF_DAY); + final String dayPeriod = hour >= 5 && hour <= 12 ? "morning" : (hour <= 18 ? "afternoon" : "evening"); + + final PrettyDate time2 = new PrettyDate(time); + + final int r1 = ThreadLocalRandom.current().nextInt(0, GREETINGS_SENTENCES.length); + final int r2 = ThreadLocalRandom.current().nextInt(0, SENTENCE_ENDS.length); + final int r3 = ThreadLocalRandom.current().nextInt(0, DATE_SENTENCES.length); + final int r4 = ThreadLocalRandom.current().nextInt(0, SENTENCE_ENDS.length); + final int r5 = ThreadLocalRandom.current().nextInt(0, PRIME_SENTENCES.length); + final int r6 = ThreadLocalRandom.current().nextInt(0, SENTENCE_ENDS.length); + final int r7 = ThreadLocalRandom.current().nextInt(0, END_SENTENCES.length); + + // GREETINGS + + String greetings = String.format(GREETINGS_SENTENCES[r1], dayPeriod); + if (!greetings.endsWith("?")) + greetings += SENTENCE_ENDS[r2]; + + // DATE + + String date = String.format(DATE_SENTENCES[r3], time2.getPrettyDate(), time2.getPrettyTime()); + if (greetings.endsWith(",")) + date = date.substring(0, 1).toLowerCase() + date.substring(1); + date += SENTENCE_ENDS[r4]; + if (date.endsWith(",")) + date = date.substring(0, date.length() - 1) + " and"; + + // PRIME + + String prime = String.format(PRIME_SENTENCES[r5], NumberFormat.getNumberInstance().format(timeValue)); + if (date.endsWith("d")) + prime = prime.substring(0, 1).toLowerCase() + prime.substring(1); + + if (END_SENTENCES[r7].length() == 0 && SENTENCE_ENDS[r6] == ',') + prime += "."; + else + prime += SENTENCE_ENDS[r6]; + + // END + + String end = END_SENTENCES[r7]; + if (prime.endsWith(",")) + end = end.substring(0, 1).toLowerCase() + end.substring(1); + + return greetings + " " + date + " " + prime + " " + end; + } + + public static void main(String[] args) { + + Locale.setDefault(Locale.ENGLISH); + + final int max_random = GREETINGS_SENTENCES.length * DATE_SENTENCES.length * PRIME_SENTENCES.length + * END_SENTENCES.length; + + System.out.println(String.format("PrimeDate %s%n%s%n%s+ sentences available randomly", VERSION, + Calendar.getInstance().getTime(), NumberFormat.getInstance().format(max_random))); + + if (args.length < 1) { + System.out.println("Argument 1 must be a file containing customer keys"); + System.exit(0); + } + + if (TwitterClient.setUpTwitter(args[0])) { + + PrimeCalculator.computeList(); + + Timer timer = new Timer(); + timer.scheduleAtFixedRate(new TimerTask() { + + @Override + public void run() { + Calendar currentTime = Calendar.getInstance(); + currentTime.add(Calendar.MILLISECOND, + -currentTime.getTimeZone().getOffset(currentTime.getTimeInMillis())); + checkTime(currentTime, false); + } + }, 0, 1 * 60 * 1000); + + /* + * Calendar time = Calendar.getInstance(); while (true) { checkTime(time, true); + * time.add(Calendar.MINUTE, 1); } + */ + } + } + +} diff --git a/src/fr/klemek/primedate/PrettyDate.java b/src/main/java/fr/klemek/primedate/PrettyDate.java similarity index 94% rename from src/fr/klemek/primedate/PrettyDate.java rename to src/main/java/fr/klemek/primedate/PrettyDate.java index 663ac04..e439ecb 100644 --- a/src/fr/klemek/primedate/PrettyDate.java +++ b/src/main/java/fr/klemek/primedate/PrettyDate.java @@ -1,42 +1,42 @@ -package fr.klemek.primedate; - -import java.util.Calendar; - -public class PrettyDate { - - private Calendar cal; - - public PrettyDate() { - cal = Calendar.getInstance(); - } - - public PrettyDate(Calendar cal) { - this.cal = cal; - } - - private String getPrettyDayOfMonth() { - final int n = cal.get(Calendar.DAY_OF_MONTH); - if (n >= 11 && n <= 13) { - return n+"th"; - } - switch (n % 10) { - case 1: - return n + "st"; - case 2: - return n + "nd"; - case 3: - return n + "rd"; - default: - return n + "th"; - } - } - - public String getPrettyDate() { - return String.format("%1$tB the %2$s, %1$tY", cal.getTime(), getPrettyDayOfMonth()); - } - - public String getPrettyTime() { - return String.format("%1$tH:%1$tM GMT", cal.getTime()); - } - -} +package fr.klemek.primedate; + +import java.util.Calendar; + +public class PrettyDate { + + private Calendar cal; + + public PrettyDate() { + cal = Calendar.getInstance(); + } + + public PrettyDate(Calendar cal) { + this.cal = cal; + } + + private String getPrettyDayOfMonth() { + final int n = cal.get(Calendar.DAY_OF_MONTH); + if (n >= 11 && n <= 13) { + return n+"th"; + } + switch (n % 10) { + case 1: + return n + "st"; + case 2: + return n + "nd"; + case 3: + return n + "rd"; + default: + return n + "th"; + } + } + + public String getPrettyDate() { + return String.format("%1$tB the %2$s, %1$tY", cal.getTime(), getPrettyDayOfMonth()); + } + + public String getPrettyTime() { + return String.format("%1$tH:%1$tM GMT", cal.getTime()); + } + +} diff --git a/src/fr/klemek/primedate/PrimeCalculator.java b/src/main/java/fr/klemek/primedate/PrimeCalculator.java similarity index 95% rename from src/fr/klemek/primedate/PrimeCalculator.java rename to src/main/java/fr/klemek/primedate/PrimeCalculator.java index 1738843..45dad78 100644 --- a/src/fr/klemek/primedate/PrimeCalculator.java +++ b/src/main/java/fr/klemek/primedate/PrimeCalculator.java @@ -1,129 +1,129 @@ -package fr.klemek.primedate; - -import java.util.Arrays; - -/** - * Calculate and check prime numbers - * - * @author Kleme - */ -public abstract class PrimeCalculator { - - // http://compoasso.free.fr/primelistweb/page/prime/eratosthene_en.php - - private static final long MAX = 1000000L; // 1 000 000 - private static final long SQRT_MAX = (long) Math.sqrt(MAX) + 1; - private static final int MEMORY_SIZE = (int) (MAX >> 4); - private static final int BLOCK_MAX = (int) (1 << 4); - - private static boolean computed = false; - private static byte[] primes = new byte[MEMORY_SIZE]; - - /** - * Get stored bit for number i - * - * @param i - * @return - */ - private static boolean getBit(long i) { - byte block = primes[(int) (i >> 4)]; - byte mask = (byte) (1 << ((i >> 1) & 7)); - return ((block & mask) != 0); - } - - /** - * Set stored bit for number i - * - * @param i - */ - private static void setBit(long i) { - int index = (int) (i >> 4); - byte block = primes[index]; - byte mask = (byte) (1 << ((i >> 1) & 7)); - primes[index] = (byte) (block | mask); - } - - /** - * Computes first million numbers, (takes ~25 ms) - */ - public static void computeList() { - Arrays.fill(primes, (byte) 0); - long t0 = System.currentTimeMillis(); - for (long i = 3; i < SQRT_MAX; i += 2) - if (!getBit(i)) { - long j = (i * i); - while (j < MAX) { - setBit(j); - j += (2 * i); - } - } - computed = true; - System.out.println( - String.format("Calculated %d KB of primes in %d ms", MEMORY_SIZE / 1000, System.currentTimeMillis() - t0)); - } - - /** - * Next prime stored - * - * @param p - * @return - */ - private static int nextPrime(int p) { - p++; - while (getBit(p)) - p++; - return p; - } - - /** - * Check if a number is prime by calculating its block - * - * @param number - * @return - */ - private static boolean isPrimeByBlock(long number) { - long start = number - number % BLOCK_MAX; - long end = start + BLOCK_MAX; - long endSqrt = (long) Math.sqrt(end) + 1; - - byte block = 0; - int p = 2; - while (p < endSqrt) { - p = nextPrime(p); - - long j = p - (start % p); - j += ((j & 1) == 0 ? p : 0); - - long p2 = p * 2; - while (j < BLOCK_MAX) { - block |= (1 << ((j >> 1) & 7)); - j += p2; - } - } - byte mask = (byte) (1 << ((number >> 1) & 7)); - - return !((block & mask) != 0); - } - - /** - * Check if a number is prime (max 1 000 000 000 000) - * - * @param number - * @return - * @throws Exception - */ - public static boolean isPrime(long number) throws Exception { - if (number <= 0 || Math.sqrt(number) > MAX) - throw new Exception("Number out of range"); - if (number <= 2) - return true; - if ((number & 1) == 0) - return false; - if (!computed) - computeList(); - if (number < MAX) - return !getBit(number); - return isPrimeByBlock(number); - } - -} +package fr.klemek.primedate; + +import java.util.Arrays; + +/** + * Calculate and check prime numbers + * + * @author Kleme + */ +public abstract class PrimeCalculator { + + // http://compoasso.free.fr/primelistweb/page/prime/eratosthene_en.php + + private static final long MAX = 1000000L; // 1 000 000 + private static final long SQRT_MAX = (long) Math.sqrt(MAX) + 1; + private static final int MEMORY_SIZE = (int) (MAX >> 4); + private static final int BLOCK_MAX = (int) (1 << 4); + + private static boolean computed = false; + private static byte[] primes = new byte[MEMORY_SIZE]; + + /** + * Get stored bit for number i + * + * @param i + * @return + */ + private static boolean getBit(long i) { + byte block = primes[(int) (i >> 4)]; + byte mask = (byte) (1 << ((i >> 1) & 7)); + return ((block & mask) != 0); + } + + /** + * Set stored bit for number i + * + * @param i + */ + private static void setBit(long i) { + int index = (int) (i >> 4); + byte block = primes[index]; + byte mask = (byte) (1 << ((i >> 1) & 7)); + primes[index] = (byte) (block | mask); + } + + /** + * Computes first million numbers, (takes ~25 ms) + */ + public static void computeList() { + Arrays.fill(primes, (byte) 0); + long t0 = System.currentTimeMillis(); + for (long i = 3; i < SQRT_MAX; i += 2) + if (!getBit(i)) { + long j = (i * i); + while (j < MAX) { + setBit(j); + j += (2 * i); + } + } + computed = true; + System.out.println( + String.format("Calculated %d KB of primes in %d ms", MEMORY_SIZE / 1000, System.currentTimeMillis() - t0)); + } + + /** + * Next prime stored + * + * @param p + * @return + */ + private static int nextPrime(int p) { + p++; + while (getBit(p)) + p++; + return p; + } + + /** + * Check if a number is prime by calculating its block + * + * @param number + * @return + */ + private static boolean isPrimeByBlock(long number) { + long start = number - number % BLOCK_MAX; + long end = start + BLOCK_MAX; + long endSqrt = (long) Math.sqrt(end) + 1; + + byte block = 0; + int p = 2; + while (p < endSqrt) { + p = nextPrime(p); + + long j = p - (start % p); + j += ((j & 1) == 0 ? p : 0); + + long p2 = p * 2; + while (j < BLOCK_MAX) { + block |= (1 << ((j >> 1) & 7)); + j += p2; + } + } + byte mask = (byte) (1 << ((number >> 1) & 7)); + + return !((block & mask) != 0); + } + + /** + * Check if a number is prime (max 1 000 000 000 000) + * + * @param number + * @return + * @throws Exception + */ + public static boolean isPrime(long number) throws Exception { + if (number <= 0 || Math.sqrt(number) > MAX) + throw new Exception("Number out of range"); + if (number <= 2) + return true; + if ((number & 1) == 0) + return false; + if (!computed) + computeList(); + if (number < MAX) + return !getBit(number); + return isPrimeByBlock(number); + } + +} diff --git a/src/fr/klemek/primedate/TwitterClient.java b/src/main/java/fr/klemek/primedate/TwitterClient.java similarity index 96% rename from src/fr/klemek/primedate/TwitterClient.java rename to src/main/java/fr/klemek/primedate/TwitterClient.java index f23df7f..2caf938 100644 --- a/src/fr/klemek/primedate/TwitterClient.java +++ b/src/main/java/fr/klemek/primedate/TwitterClient.java @@ -1,179 +1,179 @@ -package fr.klemek.primedate; - -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.OutputStreamWriter; -import java.util.ArrayList; - -import twitter4j.Twitter; -import twitter4j.TwitterException; -import twitter4j.TwitterFactory; -import twitter4j.auth.AccessToken; -import twitter4j.auth.RequestToken; -import twitter4j.conf.ConfigurationBuilder; - -/** - * Contains the useful functions to connect to twitter - * @author Kleme - */ -public abstract class TwitterClient { - - //http://twitter4j.org/en/code-examples.html - - private final static String ACCESS_TOKEN_FILE = "access_token.txt"; - - private static Twitter twitter; - - /** - * Load keys and tokens to connect to the correct twitter account - * @param keyFile the file containing the customer keys - * @return - */ - public static boolean setUpTwitter(String keyFile) { - try { - File consumer_file = new File(keyFile); - if(!consumer_file.exists()) { - System.out.println("Invalid consumer key file"); - return false; - } - - String[] consumer_key = loadConsumerKey(keyFile); - - if(consumer_key.length < 2) { - System.out.println("Invalid consumer key file"); - return false; - } - - File token_file = new File(ACCESS_TOKEN_FILE); - - if(!token_file.exists()) { - twitter = TwitterFactory.getSingleton(); - twitter.setOAuthConsumer(consumer_key[0], consumer_key[1]); - RequestToken requestToken = twitter.getOAuthRequestToken(); - AccessToken accessToken = null; - BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); - while (null == accessToken) { - System.out.println("Open the following URL and grant access to your account:"); - System.out.println(requestToken.getAuthorizationURL()); - System.out.print("Enter the PIN(if available) or just hit enter.[PIN]:"); - String pin; - - pin = br.readLine(); - if (pin.length() > 0) { - accessToken = twitter.getOAuthAccessToken(requestToken, pin); - } else { - accessToken = twitter.getOAuthAccessToken(); - } - } - // persist to the accessToken for future reference. - storeAccessToken(accessToken); - }else { - /*TwitterFactory factory = new TwitterFactory(); - - twitter = factory.getInstance(); - twitter.setOAuthConsumer(consumer_key[0], consumer_key[1]); - twitter.setOAuthAccessToken(accessToken);*/ - AccessToken accessToken = loadAccessToken(); - ConfigurationBuilder cb = new ConfigurationBuilder(); - cb.setDebugEnabled(true) - .setOAuthConsumerKey(consumer_key[0]) - .setOAuthConsumerSecret(consumer_key[1]) - .setOAuthAccessToken(accessToken.getToken()) - .setOAuthAccessTokenSecret(accessToken.getTokenSecret()); - TwitterFactory tf = new TwitterFactory(cb.build()); - twitter = tf.getInstance(); - } - } catch (IOException e) { - e.printStackTrace(); - return false; - } catch (TwitterException te) { - if (401 == te.getStatusCode()) { - System.out.println("Unable to get the access token."); - } else { - te.printStackTrace(); - } - return false; - } - - System.out.println("Successfuly connected to twitter"); - - return true; - } - - /** - * Store the access tokens into the constant file - * @param accessToken - * @throws IOException - */ - private static void storeAccessToken(AccessToken accessToken) throws IOException { - BufferedWriter bw = null; - try { - File file = new File(ACCESS_TOKEN_FILE); - file.createNewFile(); - bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file))); - bw.write(accessToken.getToken()); - bw.newLine(); - bw.write(accessToken.getTokenSecret()); - }finally { - bw.close(); - } - } - - /** - * Load the access tokens from the constant file - * @return - * @throws IOException - */ - private static AccessToken loadAccessToken() throws IOException { - BufferedReader br = null; - try { - File file = new File(ACCESS_TOKEN_FILE); - br = new BufferedReader(new InputStreamReader(new FileInputStream(file))); - String token = br.readLine(); - String tokenSecret = br.readLine(); - return new AccessToken(token, tokenSecret); - }finally { - br.close(); - } - } - - /** - * Load the consumer keys from the given file - * @param file_name - * @return - * @throws IOException - */ - private static String[] loadConsumerKey(String file_name) throws IOException{ - BufferedReader br = null; - try { - ArrayList file_lines = new ArrayList<>(); - File file = new File(file_name); - br = new BufferedReader(new InputStreamReader(new FileInputStream(file))); - String line; - while((line = br.readLine()) != null) { - file_lines.add(line); - } - return file_lines.toArray(new String[0]); - }finally { - br.close(); - } - } - - /** - * Update the status of the twitter account - * @param msg - */ - public static void tweet(String msg) { - try { - twitter.updateStatus(msg); - System.out.println("Tweeted : "+msg); - } catch (TwitterException e) { - e.printStackTrace(); - } - } -} +package fr.klemek.primedate; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.util.ArrayList; + +import twitter4j.Twitter; +import twitter4j.TwitterException; +import twitter4j.TwitterFactory; +import twitter4j.auth.AccessToken; +import twitter4j.auth.RequestToken; +import twitter4j.conf.ConfigurationBuilder; + +/** + * Contains the useful functions to connect to twitter + * @author Kleme + */ +public abstract class TwitterClient { + + //http://twitter4j.org/en/code-examples.html + + private final static String ACCESS_TOKEN_FILE = "access_token.txt"; + + private static Twitter twitter; + + /** + * Load keys and tokens to connect to the correct twitter account + * @param keyFile the file containing the customer keys + * @return + */ + public static boolean setUpTwitter(String keyFile) { + try { + File consumer_file = new File(keyFile); + if(!consumer_file.exists()) { + System.out.println("Invalid consumer key file"); + return false; + } + + String[] consumer_key = loadConsumerKey(keyFile); + + if(consumer_key.length < 2) { + System.out.println("Invalid consumer key file"); + return false; + } + + File token_file = new File(ACCESS_TOKEN_FILE); + + if(!token_file.exists()) { + twitter = TwitterFactory.getSingleton(); + twitter.setOAuthConsumer(consumer_key[0], consumer_key[1]); + RequestToken requestToken = twitter.getOAuthRequestToken(); + AccessToken accessToken = null; + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + while (null == accessToken) { + System.out.println("Open the following URL and grant access to your account:"); + System.out.println(requestToken.getAuthorizationURL()); + System.out.print("Enter the PIN(if available) or just hit enter.[PIN]:"); + String pin; + + pin = br.readLine(); + if (pin.length() > 0) { + accessToken = twitter.getOAuthAccessToken(requestToken, pin); + } else { + accessToken = twitter.getOAuthAccessToken(); + } + } + // persist to the accessToken for future reference. + storeAccessToken(accessToken); + }else { + /*TwitterFactory factory = new TwitterFactory(); + + twitter = factory.getInstance(); + twitter.setOAuthConsumer(consumer_key[0], consumer_key[1]); + twitter.setOAuthAccessToken(accessToken);*/ + AccessToken accessToken = loadAccessToken(); + ConfigurationBuilder cb = new ConfigurationBuilder(); + cb.setDebugEnabled(true) + .setOAuthConsumerKey(consumer_key[0]) + .setOAuthConsumerSecret(consumer_key[1]) + .setOAuthAccessToken(accessToken.getToken()) + .setOAuthAccessTokenSecret(accessToken.getTokenSecret()); + TwitterFactory tf = new TwitterFactory(cb.build()); + twitter = tf.getInstance(); + } + } catch (IOException e) { + e.printStackTrace(); + return false; + } catch (TwitterException te) { + if (401 == te.getStatusCode()) { + System.out.println("Unable to get the access token."); + } else { + te.printStackTrace(); + } + return false; + } + + System.out.println("Successfuly connected to twitter"); + + return true; + } + + /** + * Store the access tokens into the constant file + * @param accessToken + * @throws IOException + */ + private static void storeAccessToken(AccessToken accessToken) throws IOException { + BufferedWriter bw = null; + try { + File file = new File(ACCESS_TOKEN_FILE); + file.createNewFile(); + bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file))); + bw.write(accessToken.getToken()); + bw.newLine(); + bw.write(accessToken.getTokenSecret()); + }finally { + bw.close(); + } + } + + /** + * Load the access tokens from the constant file + * @return + * @throws IOException + */ + private static AccessToken loadAccessToken() throws IOException { + BufferedReader br = null; + try { + File file = new File(ACCESS_TOKEN_FILE); + br = new BufferedReader(new InputStreamReader(new FileInputStream(file))); + String token = br.readLine(); + String tokenSecret = br.readLine(); + return new AccessToken(token, tokenSecret); + }finally { + br.close(); + } + } + + /** + * Load the consumer keys from the given file + * @param file_name + * @return + * @throws IOException + */ + private static String[] loadConsumerKey(String file_name) throws IOException{ + BufferedReader br = null; + try { + ArrayList file_lines = new ArrayList<>(); + File file = new File(file_name); + br = new BufferedReader(new InputStreamReader(new FileInputStream(file))); + String line; + while((line = br.readLine()) != null) { + file_lines.add(line); + } + return file_lines.toArray(new String[0]); + }finally { + br.close(); + } + } + + /** + * Update the status of the twitter account + * @param msg + */ + public static void tweet(String msg) { + try { + twitter.updateStatus(msg); + System.out.println("Tweeted : "+msg); + } catch (TwitterException e) { + e.printStackTrace(); + } + } +} diff --git a/test/fr/klemek/primedate/PrimeCalculatorTests.java b/src/test/java/fr/klemek/primedate/PrimeCalculatorTests.java similarity index 95% rename from test/fr/klemek/primedate/PrimeCalculatorTests.java rename to src/test/java/fr/klemek/primedate/PrimeCalculatorTests.java index 7df54db..965c22b 100644 --- a/test/fr/klemek/primedate/PrimeCalculatorTests.java +++ b/src/test/java/fr/klemek/primedate/PrimeCalculatorTests.java @@ -1,44 +1,44 @@ -package fr.klemek.primedate; - -import static org.junit.Assert.*; - -import org.junit.Test; - -public class PrimeCalculatorTests { - - public void testNumber(boolean expected, long number) throws Exception { - if(expected) - assertTrue(number+" should be prime", PrimeCalculator.isPrime(number)); - else - assertFalse(number+" should not be prime", PrimeCalculator.isPrime(number)); - } - - @Test - public void testIsPrimeSmall() throws Exception { - testNumber(true, 1); - testNumber(true, 2); - testNumber(true, 3); - testNumber(false, 4); - testNumber(true, 5); - testNumber(false, 6); - testNumber(true, 7); - testNumber(false, 9); - } - - @Test - public void testIsPrimeNormal() throws Exception { - testNumber(true, 8011); - testNumber(true, 8941); - testNumber(false, 8943); - testNumber(true, 9283); - } - - @Test - public void testIsPrimeBig() throws Exception { - testNumber(true, 201802181381L); - testNumber(false, 201802181383L); - testNumber(false, 201802181307L); - testNumber(false, 201802181409L); - testNumber(true, 201802181411L); - } -} +package fr.klemek.primedate; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class PrimeCalculatorTests { + + public void testNumber(boolean expected, long number) throws Exception { + if(expected) + assertTrue(number+" should be prime", PrimeCalculator.isPrime(number)); + else + assertFalse(number+" should not be prime", PrimeCalculator.isPrime(number)); + } + + @Test + public void testIsPrimeSmall() throws Exception { + testNumber(true, 1); + testNumber(true, 2); + testNumber(true, 3); + testNumber(false, 4); + testNumber(true, 5); + testNumber(false, 6); + testNumber(true, 7); + testNumber(false, 9); + } + + @Test + public void testIsPrimeNormal() throws Exception { + testNumber(true, 8011); + testNumber(true, 8941); + testNumber(false, 8943); + testNumber(true, 9283); + } + + @Test + public void testIsPrimeBig() throws Exception { + testNumber(true, 201802181381L); + testNumber(false, 201802181383L); + testNumber(false, 201802181307L); + testNumber(false, 201802181409L); + testNumber(true, 201802181411L); + } +}