From c1aba95f9f35f9c7b9a335841d9474a29c642747 Mon Sep 17 00:00:00 2001 From: neomatrix369 Date: Sat, 9 Feb 2013 11:44:15 +0000 Subject: [PATCH 01/24] Push JSR-353-JSON project into repo. --- .classpath | 39 ++++++++ .project | 23 +++++ pom.xml | 37 +++++++ .../java/JSR353/JSON/PricesCacheStore.java | 39 ++++++++ .../java/JSR353/JSON/YahooFinanceStub.java | 99 +++++++++++++++++++ 5 files changed, 237 insertions(+) create mode 100644 .classpath create mode 100644 .project create mode 100644 pom.xml create mode 100644 src/main/java/JSR353/JSON/PricesCacheStore.java create mode 100644 src/main/java/JSR353/JSON/YahooFinanceStub.java diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..4059d42 --- /dev/null +++ b/.classpath @@ -0,0 +1,39 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.project b/.project new file mode 100644 index 0000000..f130ce4 --- /dev/null +++ b/.project @@ -0,0 +1,23 @@ + + + JSR-353-JSON + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..eddfbd2 --- /dev/null +++ b/pom.xml @@ -0,0 +1,37 @@ + + 4.0.0 + YahooFinanceStub + YahooFinanceStub + 0.0.1-SNAPSHOT + YahooFinanceStub + Stub to generate prices and store in the cache! + + + Java EE 7 + https://maven.java.net/content/groups/promoted/ + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 2.3.2 + + 1.7 + 1.7 + + + + + + + \ No newline at end of file diff --git a/src/main/java/JSR353/JSON/PricesCacheStore.java b/src/main/java/JSR353/JSON/PricesCacheStore.java new file mode 100644 index 0000000..72a0c9c --- /dev/null +++ b/src/main/java/JSR353/JSON/PricesCacheStore.java @@ -0,0 +1,39 @@ +package JSR353.JSON; + +import java.util.concurrent.ConcurrentHashMap; + +public class PricesCacheStore { + + private final static PricesCacheStore INSTANCE = new PricesCacheStore(); + + private PricesCacheStore() {} + private volatile String lastSymboleUpdated; + + // will contain Symbols and prices stored and constantly updated + private volatile ConcurrentHashMap pricesCache = new ConcurrentHashMap<>(); + + public static PricesCacheStore getInstance() { + return INSTANCE; + } + + public void addPrice(String symbol, Double price) { + pricesCache.put(symbol, price); + lastSymboleUpdated = symbol; + } + + public Double getPrice(String symbol) { + Double price = pricesCache.get(symbol); + return price == null ? 0 : price; + } + + // Keep in mind immutability - return a copy of the object + // does not matter if it is static or volatile + // or if its threading is safe + public ConcurrentHashMap getAllPrices() { + return new ConcurrentHashMap(pricesCache); + } + + public String getLastSymbolUpdated() { + return new String(lastSymboleUpdated); + } +} \ No newline at end of file diff --git a/src/main/java/JSR353/JSON/YahooFinanceStub.java b/src/main/java/JSR353/JSON/YahooFinanceStub.java new file mode 100644 index 0000000..5489cfa --- /dev/null +++ b/src/main/java/JSR353/JSON/YahooFinanceStub.java @@ -0,0 +1,99 @@ +package JSR353.JSON; +import java.util.Random; +import java.util.concurrent.ConcurrentHashMap; + +import javax.json.*; +import javax.json.stream.*; + +public class YahooFinanceStub implements Runnable { + + private String[] symbols = new String[] {"GOOG", "MSFT", "YHOO", "IBM"}; + + @Override + public void run() { + boolean stopRunning = false; + // Poll yahoo - generate random price for the moment + // <-code to poll Yahoo-> + + // Simulate real-time prices + while( !stopRunning ) { + // Post results into cache store + PricesCacheStore pricesCacheStore = PricesCacheStore.getInstance(); + pricesCacheStore.addPrice(getRandomSymbol(), getRandomPrice()); + + // get the last symbol updated + String symbol = pricesCacheStore.getLastSymbolUpdated(); + + // print it to console + writeJSONObjectToConsoleOnlyOnce(symbol, pricesCacheStore.getPrice(symbol)); + + // Then pause for a bit...before starting all over again + try { + Thread.sleep(1000); + + // if user types a key in the console, then stop running! + //if ( readUserInput() != "" ) stopRunning = true; + } catch (InterruptedException ex) { + throw new RuntimeException( ex ); + } + } + } + + // Implementation of JsonGenerator in JSR-353 + private void writeJSONObjectToConsoleOnlyOnce(String symbol, Double price) { + JsonGenerator generator = Json.createGenerator(System.out); + if (generator != null) { + generator + .writeStartObject() + .write("symbol", symbol) + .write("price", price) + .writeEnd(); + + generator.close(); + } + } + + private void writeJSONObjectToConsoleRepeatedly(String symbol, Double price) { + //JsonObject value = Json.createReader(inputStream).readObject(); + } + + // Implementation of JsonObject & Json.createObjectBuilder in JSR-353 + private JsonObject getJSONObject(String symbol, Double price) { + /* JsonObject value = Json.createObjectBuilder() + //.add("quotes") + .add("symbol", symbol) + .add("price", price) + .build(); + */ + JsonObject value = null; + return value; + } + + + public String getRandomSymbol() { + String symbol = ""; + if ((symbols != null) && (symbols.length > 0)) { + Random randSeed = new Random(); + int symbolsUpperLimit= symbols.length; + int randomIndex = Math.abs(randSeed.nextInt()) % symbolsUpperLimit; + symbol = symbols[randomIndex]; + } + return symbol; + } + public ConcurrentHashMap getAllPrices() { + return PricesCacheStore.getInstance().getAllPrices(); + } + + public static void main(String[] args) { + + YahooFinanceStub yahooStub = new YahooFinanceStub(); + + Thread yahooPricesThread = new Thread(yahooStub); + yahooPricesThread.start(); + } + + static private Double getRandomPrice() { + Random rand = new Random(); + return rand.nextDouble() * 100; + } +} \ No newline at end of file From e1c651d8473cb3b6a0fd8cf71bbbc0d8429fc542 Mon Sep 17 00:00:00 2001 From: neomatrix369 Date: Sat, 9 Feb 2013 12:57:27 +0000 Subject: [PATCH 02/24] Pom changes & further Json implementations. --- .classpath | 3 --- pom.xml | 4 ++-- src/main/java/JSR353/JSON/YahooFinanceStub.java | 15 +++++++++++---- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/.classpath b/.classpath index 4059d42..e43402f 100644 --- a/.classpath +++ b/.classpath @@ -32,8 +32,5 @@ - - - diff --git a/pom.xml b/pom.xml index eddfbd2..ebd647e 100644 --- a/pom.xml +++ b/pom.xml @@ -27,11 +27,11 @@ - + \ No newline at end of file diff --git a/src/main/java/JSR353/JSON/YahooFinanceStub.java b/src/main/java/JSR353/JSON/YahooFinanceStub.java index 5489cfa..19537eb 100644 --- a/src/main/java/JSR353/JSON/YahooFinanceStub.java +++ b/src/main/java/JSR353/JSON/YahooFinanceStub.java @@ -23,9 +23,11 @@ public void run() { // get the last symbol updated String symbol = pricesCacheStore.getLastSymbolUpdated(); + Double price = pricesCacheStore.getPrice(symbol); // print it to console - writeJSONObjectToConsoleOnlyOnce(symbol, pricesCacheStore.getPrice(symbol)); + //writeJSONObjectToConsoleOnlyOnce(symbol, price); + System.out.println(getJSONObjectFromValues(symbol, price)); // Then pause for a bit...before starting all over again try { @@ -39,7 +41,7 @@ public void run() { } } - // Implementation of JsonGenerator in JSR-353 + // Implementation of JsonGenerator and Json in JSR-353 private void writeJSONObjectToConsoleOnlyOnce(String symbol, Double price) { JsonGenerator generator = Json.createGenerator(System.out); if (generator != null) { @@ -53,8 +55,13 @@ private void writeJSONObjectToConsoleOnlyOnce(String symbol, Double price) { } } - private void writeJSONObjectToConsoleRepeatedly(String symbol, Double price) { - //JsonObject value = Json.createReader(inputStream).readObject(); + // Implementation of JsonGenerator and Json in JSR-353 + private JsonObject getJSONObjectFromValues(String symbol, Double price) { + JsonObject value = new JsonObjectBuilder() + .add("symbol", symbol) + .add("price", price) + .build(); + return value; } // Implementation of JsonObject & Json.createObjectBuilder in JSR-353 From 9c740f7f7af3a85cc3f80258caf6ed3edde41ed1 Mon Sep 17 00:00:00 2001 From: neomatrix369 Date: Sat, 9 Feb 2013 13:16:43 +0000 Subject: [PATCH 03/24] Pom.xml changes. --- pom.xml | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/pom.xml b/pom.xml index ebd647e..16106dd 100644 --- a/pom.xml +++ b/pom.xml @@ -29,9 +29,15 @@ - javax - javaee-api - 7.0-b72 + org.glassfish + javax.json + 1.0-b02 - + + + \ No newline at end of file From a43627b7b297a56ad8c3342958a33e1144ac59fb Mon Sep 17 00:00:00 2001 From: neomatrix369 Date: Sat, 9 Feb 2013 14:58:17 +0000 Subject: [PATCH 04/24] new implementations of Json. --- .../java/JSR353/JSON/YahooFinanceStub.java | 31 +++++++------------ 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/src/main/java/JSR353/JSON/YahooFinanceStub.java b/src/main/java/JSR353/JSON/YahooFinanceStub.java index 19537eb..c6f9d16 100644 --- a/src/main/java/JSR353/JSON/YahooFinanceStub.java +++ b/src/main/java/JSR353/JSON/YahooFinanceStub.java @@ -1,4 +1,11 @@ package JSR353.JSON; +import java.io.BufferedWriter; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; import java.util.Random; import java.util.concurrent.ConcurrentHashMap; @@ -26,15 +33,15 @@ public void run() { Double price = pricesCacheStore.getPrice(symbol); // print it to console - //writeJSONObjectToConsoleOnlyOnce(symbol, price); - System.out.println(getJSONObjectFromValues(symbol, price)); + // Uncomment the below to test this method - but yo + writeJSONObjectToConsoleOnlyOnce(symbol, price); + + //System.out.println(getJSONObjectFromValues(symbol, price)); // Then pause for a bit...before starting all over again try { Thread.sleep(1000); - // if user types a key in the console, then stop running! - //if ( readUserInput() != "" ) stopRunning = true; } catch (InterruptedException ex) { throw new RuntimeException( ex ); } @@ -44,7 +51,6 @@ public void run() { // Implementation of JsonGenerator and Json in JSR-353 private void writeJSONObjectToConsoleOnlyOnce(String symbol, Double price) { JsonGenerator generator = Json.createGenerator(System.out); - if (generator != null) { generator .writeStartObject() .write("symbol", symbol) @@ -52,10 +58,9 @@ private void writeJSONObjectToConsoleOnlyOnce(String symbol, Double price) { .writeEnd(); generator.close(); - } } - // Implementation of JsonGenerator and Json in JSR-353 + // Implementation of JsonObject & JsonObjectBuilder in JSR-353 private JsonObject getJSONObjectFromValues(String symbol, Double price) { JsonObject value = new JsonObjectBuilder() .add("symbol", symbol) @@ -64,18 +69,6 @@ private JsonObject getJSONObjectFromValues(String symbol, Double price) { return value; } - // Implementation of JsonObject & Json.createObjectBuilder in JSR-353 - private JsonObject getJSONObject(String symbol, Double price) { - /* JsonObject value = Json.createObjectBuilder() - //.add("quotes") - .add("symbol", symbol) - .add("price", price) - .build(); - */ - JsonObject value = null; - return value; - } - public String getRandomSymbol() { String symbol = ""; From 254dbb809b9eb68715b0d37520883fb73da0dbd2 Mon Sep 17 00:00:00 2001 From: sivajik Date: Sat, 9 Feb 2013 15:16:19 +0000 Subject: [PATCH 05/24] JSON Reader, write implementation. --- .classpath | 16 ---- jsr353.txt | 1 + pom.xml | 14 ++- .../java/JSR353/JSON/YahooFinanceStub.java | 88 ++++++++++++++++++- 4 files changed, 95 insertions(+), 24 deletions(-) create mode 100644 jsr353.txt diff --git a/.classpath b/.classpath index e43402f..2dd4731 100644 --- a/.classpath +++ b/.classpath @@ -6,22 +6,6 @@ - - - - - - - - - - - - - - - - diff --git a/jsr353.txt b/jsr353.txt new file mode 100644 index 0000000..8c2b7f5 --- /dev/null +++ b/jsr353.txt @@ -0,0 +1 @@ +{"symbol":"IBM","price":54.21678079488639} \ No newline at end of file diff --git a/pom.xml b/pom.xml index ebd647e..16106dd 100644 --- a/pom.xml +++ b/pom.xml @@ -29,9 +29,15 @@ - javax - javaee-api - 7.0-b72 + org.glassfish + javax.json + 1.0-b02 - + + + \ No newline at end of file diff --git a/src/main/java/JSR353/JSON/YahooFinanceStub.java b/src/main/java/JSR353/JSON/YahooFinanceStub.java index 19537eb..29b3a0d 100644 --- a/src/main/java/JSR353/JSON/YahooFinanceStub.java +++ b/src/main/java/JSR353/JSON/YahooFinanceStub.java @@ -1,17 +1,25 @@ package JSR353.JSON; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; import java.util.Random; import java.util.concurrent.ConcurrentHashMap; -import javax.json.*; -import javax.json.stream.*; +import javax.json.Json; +import javax.json.JsonObject; +import javax.json.JsonObjectBuilder; +import javax.json.JsonReader; +import javax.json.JsonWriter; +import javax.json.stream.JsonGenerator; public class YahooFinanceStub implements Runnable { private String[] symbols = new String[] {"GOOG", "MSFT", "YHOO", "IBM"}; + boolean stopRunning = false; + @Override public void run() { - boolean stopRunning = false; // Poll yahoo - generate random price for the moment // <-code to poll Yahoo-> @@ -27,7 +35,13 @@ public void run() { // print it to console //writeJSONObjectToConsoleOnlyOnce(symbol, price); - System.out.println(getJSONObjectFromValues(symbol, price)); + //System.out.println(getJSONObjectFromValues(symbol, price)); + + /* + * Writes/Reads the specified JSON object or array to the output source. + * This method needs to be called only once for a reader/writer instance. Hence killing thread. + */ + writeJSONObjectToStreamOnlyOnce(symbol, price); // Then pause for a bit...before starting all over again try { @@ -55,6 +69,13 @@ private void writeJSONObjectToConsoleOnlyOnce(String symbol, Double price) { } } + // + private void writeJSONObjectToStreamOnlyOnce(String symbol, Double price) { + writeJsonObj(getJSONObjectFromValues(symbol, price)); + readJsonObj(getFileInputStream()); + stopRunning = true; + } + // Implementation of JsonGenerator and Json in JSR-353 private JsonObject getJSONObjectFromValues(String symbol, Double price) { JsonObject value = new JsonObjectBuilder() @@ -103,4 +124,63 @@ static private Double getRandomPrice() { Random rand = new Random(); return rand.nextDouble() * 100; } + + + private static FileOutputStream outputStream = null; + private static FileInputStream inputStream = null; + + /* + * Method to get output stream for a file. + */ + public static FileOutputStream getFileOutputStream() { + try { + if (outputStream == null) { + outputStream = new FileOutputStream(new File("jsr353.txt")); + } else { + return outputStream; + } + } catch (Exception e) { + e.printStackTrace(); + } + return outputStream; + } + + /* + * Method to get input stream for a file. + */ + public static FileInputStream getFileInputStream() { + try { + if (inputStream == null) { + inputStream = new FileInputStream(new File("jsr353.txt")); + } else { + return inputStream; + } + } catch (Exception e) { + e.printStackTrace(); + } + return inputStream; + } + + /* + * Method to write JsonObj to a given output stream. + */ + public static void writeJsonObj(JsonObject jsonObject) { + JsonWriter jsonWriter = new JsonWriter(getFileOutputStream()); + System.out.println("JSR 353 - Write Object: "); + jsonWriter.writeObject(jsonObject); + System.out.println(jsonObject); + jsonWriter.close(); + } + + /* + * Method to write JsonObj from a given output stream. + */ + public static void readJsonObj(FileInputStream stream) { + JsonReader jsonReader = new JsonReader(stream); + System.out.println("JSR 353 - Read Object: "); + JsonObject jsonObject = jsonReader.readObject(); + System.out.println(jsonObject); + } + + } \ No newline at end of file From 9d8ff3b38c9280f26cca90db4380dac5b48cdcc5 Mon Sep 17 00:00:00 2001 From: mani Date: Sat, 9 Feb 2013 16:44:55 +0000 Subject: [PATCH 06/24] Update README.md --- README.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 7430811..37cd9c9 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,14 @@ JSR-353-JSON ============ -JSR-353-JSON Processing - artefact, followon from the LJC hackday \ No newline at end of file +```JSR-353-JSON Processing - artefact, follow-on from the LJC hackday``` + +WebSocket & JSON Hack Day (covering implementation for JSR 356 & JSR 353) + + -[Meetup event](http://www.meetup.com/Londonjavacommunity/events/101954512/)
+ -[Recorded presentation](http://skillsmatter.com/podcast/home/websock-json-hack-day)
+ -[Slides](http://www.slideshare.net/somaynakhal/wesocket-json-hackday)
+ -[Github](https://github.com/Adopt-A-JSR) ([JSR-353-JSON artefact](https://github.com/neomatrix369/JSR-353-JSON); JSR-356-WebSocketAPI to follow soon!)
+ -[David Illsley’s github link](https://github.com/davidillsley/json-workshop)
+ -[JSR-353 (JSON)](http://glassfish.java.net/adoptajsr/jsr353.html) / [JSON project page](http://json-processing-spec.java.net/)
+ -[JSR-356 (Websocket)](http://glassfish.java.net/adoptajsr/jsr356.html) / [WebSocket API project page](http://websocket-spec.java.net/)
From bc3e970c5a57e56d615f512ba5945cd6efa829b4 Mon Sep 17 00:00:00 2001 From: mani Date: Sat, 9 Feb 2013 16:46:13 +0000 Subject: [PATCH 07/24] Update README.md --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index 37cd9c9..9c79721 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,5 @@ WebSocket & JSON Hack Day (covering implementation for JSR 356 & JSR 353) -[Meetup event](http://www.meetup.com/Londonjavacommunity/events/101954512/)
-[Recorded presentation](http://skillsmatter.com/podcast/home/websock-json-hack-day)
-[Slides](http://www.slideshare.net/somaynakhal/wesocket-json-hackday)
- -[Github](https://github.com/Adopt-A-JSR) ([JSR-353-JSON artefact](https://github.com/neomatrix369/JSR-353-JSON); JSR-356-WebSocketAPI to follow soon!)
-[David Illsley’s github link](https://github.com/davidillsley/json-workshop)
-[JSR-353 (JSON)](http://glassfish.java.net/adoptajsr/jsr353.html) / [JSON project page](http://json-processing-spec.java.net/)
- -[JSR-356 (Websocket)](http://glassfish.java.net/adoptajsr/jsr356.html) / [WebSocket API project page](http://websocket-spec.java.net/)
From 6d6cca861c5f37b54f3fdd14c459497487a60b8d Mon Sep 17 00:00:00 2001 From: mani Date: Sat, 9 Feb 2013 17:39:13 +0000 Subject: [PATCH 08/24] Update README.md --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index 9c79721..466fd15 100644 --- a/README.md +++ b/README.md @@ -10,3 +10,11 @@ WebSocket & JSON Hack Day (covering implementation for JSR 356 & JSR 353) -[Slides](http://www.slideshare.net/somaynakhal/wesocket-json-hackday)
-[David Illsley’s github link](https://github.com/davidillsley/json-workshop)
-[JSR-353 (JSON)](http://glassfish.java.net/adoptajsr/jsr353.html) / [JSON project page](http://json-processing-spec.java.net/)
+ +

Summary of APIs covered

+ javax.json.Json + javax.json.JsonObject + javax.json.JsonObjectBuilder + javax.json.JsonReader + javax.json.JsonWriter + javax.json.stream.JsonGenerator From 48a2f4ee337bf96a20be89b6c12e41cb9b607570 Mon Sep 17 00:00:00 2001 From: neomatrix369 Date: Sat, 9 Feb 2013 18:02:49 +0000 Subject: [PATCH 09/24] Tidying up project and stub program. --- jsr353.txt | 2 +- .../java/JSR353/JSON/YahooFinanceStub.java | 87 ++++++++++++------- 2 files changed, 56 insertions(+), 33 deletions(-) diff --git a/jsr353.txt b/jsr353.txt index 5f4c4ce..55cf497 100644 --- a/jsr353.txt +++ b/jsr353.txt @@ -1 +1 @@ -{"symbol":"YHOO","price":19.383210820328898} \ No newline at end of file +{"symbol":"GOOG","price":21.02627903437534} \ No newline at end of file diff --git a/src/main/java/JSR353/JSON/YahooFinanceStub.java b/src/main/java/JSR353/JSON/YahooFinanceStub.java index c790c63..defb9eb 100644 --- a/src/main/java/JSR353/JSON/YahooFinanceStub.java +++ b/src/main/java/JSR353/JSON/YahooFinanceStub.java @@ -1,12 +1,8 @@ package JSR353.JSON; + import java.io.File; -import java.io.BufferedWriter; import java.io.FileInputStream; -import java.io.FileNotFoundException; import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; import java.util.Random; import java.util.concurrent.ConcurrentHashMap; @@ -22,11 +18,14 @@ public class YahooFinanceStub implements Runnable { private String[] symbols = new String[] {"GOOG", "MSFT", "YHOO", "IBM"}; boolean stopRunning = false; + boolean doNotRunWriteJSONObjectToStreamOnlyOnce = true; // enable this flag to execute this implementation! + boolean doNotRunWriteJSONObjectToConsoleOnlyOnce = true; // enable this flag to execute this implementation! + // enabling the above will prevent other implementations that write to stream/console multiple times from working! @Override public void run() { // Poll yahoo - generate random price for the moment - // <-code to poll Yahoo-> + // ToDO: <-code to poll Yahoo-> // Simulate real-time prices while( !stopRunning ) { @@ -38,35 +37,45 @@ public void run() { String symbol = pricesCacheStore.getLastSymbolUpdated(); Double price = pricesCacheStore.getPrice(symbol); - // print it to console - - //writeJSONObjectToConsoleOnlyOnce(symbol, price); - //System.out.println(getJSONObjectFromValues(symbol, price)); - + // print it to console + // Implementation of JsonGenerator and Json in JSR-353 + // Note that you can write to a stream only once - hence running only once! + writeJSONObjectToConsoleOnlyOnce(symbol, price); + + // Implementation of JsonWriter & JsonReader in JSR-353 /* * Writes/Reads the specified JSON object or array to the output source. - * This method needs to be called only once for a reader/writer instance. Hence killing thread. + * This method needs to be called only once for a reader/writer instance - hence running only once. */ writeJSONObjectToStreamOnlyOnce(symbol, price); - // Uncomment the below to test this method - but yo - //writeJSONObjectToConsoleOnlyOnce(symbol, price); - - //System.out.println(getJSONObjectFromValues(symbol, price)); - + // Implementation of JsonObject & JsonObjectBuilder in JSR-353 + // Returns a Json object with a symbol and price - continuously + System.out.println(getJSONObjectFromValues(symbol, price)); // Then pause for a bit...before starting all over again - try { - Thread.sleep(1000); - - } catch (InterruptedException ex) { - throw new RuntimeException( ex ); - } + pauseForABit(1000); } } - // Implementation of JsonGenerator and Json in JSR-353 + public void pauseForABit(int milliSecsToWait) { + try { + Thread.sleep(milliSecsToWait); + + } catch (InterruptedException ex) { + throw new RuntimeException( ex ); + } + } + + /* + * Method to write to console only once. + */ private void writeJSONObjectToConsoleOnlyOnce(String symbol, Double price) { + if (doNotRunWriteJSONObjectToConsoleOnlyOnce) return; + doNotRunWriteJSONObjectToConsoleOnlyOnce = true; + + pauseForABit(1000); + JsonGenerator generator = Json.createGenerator(System.out); generator .writeStartObject() @@ -77,15 +86,22 @@ private void writeJSONObjectToConsoleOnlyOnce(String symbol, Double price) { generator.close(); } - // Write it to stream only once. + /* + * Method to write to stream only once. + */ private void writeJSONObjectToStreamOnlyOnce(String symbol, Double price) { + if (doNotRunWriteJSONObjectToStreamOnlyOnce) return; + doNotRunWriteJSONObjectToStreamOnlyOnce = true; + + pauseForABit(1000); + writeJsonObj(getJSONObjectFromValues(symbol, price)); readJsonObj(getFileInputStream()); - stopRunning = true; } - - // Implementation of JsonGenerator and Json in JSR-353 - // Implementation of JsonObject & JsonObjectBuilder in JSR-353 + + /* + * Function to return a JsonObject + */ private JsonObject getJSONObjectFromValues(String symbol, Double price) { JsonObject value = new JsonObjectBuilder() .add("symbol", symbol) @@ -94,7 +110,9 @@ private JsonObject getJSONObjectFromValues(String symbol, Double price) { return value; } - + /* + * Method to get output stream for a file. + */ public String getRandomSymbol() { String symbol = ""; if ((symbols != null) && (symbols.length > 0)) { @@ -105,6 +123,10 @@ public String getRandomSymbol() { } return symbol; } + + /* + * Function to return all prices stored in PricesCachseStore - immutable, singleton + */ public ConcurrentHashMap getAllPrices() { return PricesCacheStore.getInstance().getAllPrices(); } @@ -117,6 +139,9 @@ public static void main(String[] args) { yahooPricesThread.start(); } + /* + * Function to return a randomized price. + */ static private Double getRandomPrice() { Random rand = new Random(); return rand.nextDouble() * 100; @@ -178,6 +203,4 @@ public static void readJsonObj(FileInputStream stream) { JsonObject jsonObject = jsonReader.readObject(); System.out.println(jsonObject); } - - } \ No newline at end of file From 71ef9ad29d5c62f3f2f4cfabd9803da80026144f Mon Sep 17 00:00:00 2001 From: mani Date: Sat, 9 Feb 2013 22:00:11 +0000 Subject: [PATCH 10/24] Update README.md --- README.md | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 466fd15..a7c6301 100644 --- a/README.md +++ b/README.md @@ -5,11 +5,19 @@ JSR-353-JSON WebSocket & JSON Hack Day (covering implementation for JSR 356 & JSR 353) - -[Meetup event](http://www.meetup.com/Londonjavacommunity/events/101954512/)
- -[Recorded presentation](http://skillsmatter.com/podcast/home/websock-json-hack-day)
- -[Slides](http://www.slideshare.net/somaynakhal/wesocket-json-hackday)
- -[David Illsley’s github link](https://github.com/davidillsley/json-workshop)
- -[JSR-353 (JSON)](http://glassfish.java.net/adoptajsr/jsr353.html) / [JSON project page](http://json-processing-spec.java.net/)
+ - [Meetup event](http://www.meetup.com/Londonjavacommunity/events/101954512/)
+ - [Recorded presentation](http://skillsmatter.com/podcast/home/websock-json-hack-day)
+ - [Slides](http://www.slideshare.net/somaynakhal/wesocket-json-hackday)
+ - [David Illsley’s github link](https://github.com/davidillsley/json-workshop)
+ - [JSR-353 (JSON)](http://glassfish.java.net/adoptajsr/jsr353.html) / [JSON project page](http://json-processing-spec.java.net/)
+ +Yahoo! Finance API resources (JSON) + - http://code.google.com/p/yahoo-finance-managed/wiki/YahooFinanceAPIs + - http://developer.yahoo.com/yql/ + - http://finance.yahoo.com/q?s=GOOG&ql=1 + - http://d.yimg.com/autoc.finance.yahoo.com/autoc?query=yahoo&callback=YAHOO.Finance.SymbolSuggest.ssCallback + - http://www.financialcontent.com/support/documentation/json_quote_api.php + - http://developer.yahoo.com/yql/console/?q=show%20tables&env=store://datatables.org/alltableswithkeys

Summary of APIs covered

javax.json.Json From 7c191a60e55fe04d7ecf95157a1ac421e350c31c Mon Sep 17 00:00:00 2001 From: neomatrix369 Date: Sun, 10 Feb 2013 19:41:04 +0000 Subject: [PATCH 11/24] JsonReader.close() added. --- src/main/java/JSR353/JSON/YahooFinanceStub.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/JSR353/JSON/YahooFinanceStub.java b/src/main/java/JSR353/JSON/YahooFinanceStub.java index defb9eb..4a42b14 100644 --- a/src/main/java/JSR353/JSON/YahooFinanceStub.java +++ b/src/main/java/JSR353/JSON/YahooFinanceStub.java @@ -202,5 +202,6 @@ public static void readJsonObj(FileInputStream stream) { System.out.println("JSR 353 - Read Object: "); JsonObject jsonObject = jsonReader.readObject(); System.out.println(jsonObject); + jsonReader.close(); } } \ No newline at end of file From f508a9f970e32cbbdaae0af5c6a3880e6df91656 Mon Sep 17 00:00:00 2001 From: neomatrix369 Date: Mon, 11 Feb 2013 15:10:40 +0000 Subject: [PATCH 12/24] synchronized added to a number of methods. --- src/main/java/JSR353/JSON/PricesCacheStore.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/JSR353/JSON/PricesCacheStore.java b/src/main/java/JSR353/JSON/PricesCacheStore.java index 72a0c9c..b5f51fa 100644 --- a/src/main/java/JSR353/JSON/PricesCacheStore.java +++ b/src/main/java/JSR353/JSON/PricesCacheStore.java @@ -16,12 +16,12 @@ public static PricesCacheStore getInstance() { return INSTANCE; } - public void addPrice(String symbol, Double price) { + public synchronized void addPrice(String symbol, Double price) { pricesCache.put(symbol, price); lastSymboleUpdated = symbol; } - public Double getPrice(String symbol) { + public synchronized Double getPrice(String symbol) { Double price = pricesCache.get(symbol); return price == null ? 0 : price; } @@ -29,7 +29,7 @@ public Double getPrice(String symbol) { // Keep in mind immutability - return a copy of the object // does not matter if it is static or volatile // or if its threading is safe - public ConcurrentHashMap getAllPrices() { + public synchronized ConcurrentHashMap getAllPrices() { return new ConcurrentHashMap(pricesCache); } From a37d1074ba12dbca282fb76aa66fb1ade79fd5aa Mon Sep 17 00:00:00 2001 From: neomatrix369 Date: Mon, 11 Feb 2013 16:08:40 +0000 Subject: [PATCH 13/24] Revert "synchronized added to a number of methods." This reverts commit 610eb50623f9f670da1e89c3c0c25f93d2eb1502. --- src/main/java/JSR353/JSON/PricesCacheStore.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/JSR353/JSON/PricesCacheStore.java b/src/main/java/JSR353/JSON/PricesCacheStore.java index b5f51fa..72a0c9c 100644 --- a/src/main/java/JSR353/JSON/PricesCacheStore.java +++ b/src/main/java/JSR353/JSON/PricesCacheStore.java @@ -16,12 +16,12 @@ public static PricesCacheStore getInstance() { return INSTANCE; } - public synchronized void addPrice(String symbol, Double price) { + public void addPrice(String symbol, Double price) { pricesCache.put(symbol, price); lastSymboleUpdated = symbol; } - public synchronized Double getPrice(String symbol) { + public Double getPrice(String symbol) { Double price = pricesCache.get(symbol); return price == null ? 0 : price; } @@ -29,7 +29,7 @@ public synchronized Double getPrice(String symbol) { // Keep in mind immutability - return a copy of the object // does not matter if it is static or volatile // or if its threading is safe - public synchronized ConcurrentHashMap getAllPrices() { + public ConcurrentHashMap getAllPrices() { return new ConcurrentHashMap(pricesCache); } From f79c00c7f7e8a504068acdb5f3dea4ca2bc458c6 Mon Sep 17 00:00:00 2001 From: heliofrota Date: Thu, 14 Mar 2013 00:07:22 -0300 Subject: [PATCH 14/24] some improvements --- .classpath | 6 + pom.xml | 18 +- .../java/JSR353/JSON/PricesCacheStore.java | 38 +- .../java/JSR353/JSON/YahooFinanceStub.java | 394 +++++++++--------- src/test/java/JSR353/JSON/AllTests.java | 14 + .../JSR353/JSON/PricesCacheStoreTest.java | 53 +++ .../JSR353/JSON/YahooFinanceStubTest.java | 5 + 7 files changed, 314 insertions(+), 214 deletions(-) create mode 100644 src/test/java/JSR353/JSON/AllTests.java create mode 100644 src/test/java/JSR353/JSON/PricesCacheStoreTest.java create mode 100644 src/test/java/JSR353/JSON/YahooFinanceStubTest.java diff --git a/.classpath b/.classpath index 2dd4731..f619a53 100644 --- a/.classpath +++ b/.classpath @@ -6,6 +6,12 @@
+ + + + + + diff --git a/pom.xml b/pom.xml index 16106dd..456f688 100644 --- a/pom.xml +++ b/pom.xml @@ -28,16 +28,24 @@ + + + junit + junit + 4.11 + test + + org.glassfish javax.json 1.0-b02 - - \ No newline at end of file diff --git a/src/main/java/JSR353/JSON/PricesCacheStore.java b/src/main/java/JSR353/JSON/PricesCacheStore.java index 72a0c9c..54850aa 100644 --- a/src/main/java/JSR353/JSON/PricesCacheStore.java +++ b/src/main/java/JSR353/JSON/PricesCacheStore.java @@ -2,37 +2,37 @@ import java.util.concurrent.ConcurrentHashMap; -public class PricesCacheStore { +public enum PricesCacheStore { + + INSTANCE; - private final static PricesCacheStore INSTANCE = new PricesCacheStore(); - - private PricesCacheStore() {} private volatile String lastSymboleUpdated; - - // will contain Symbols and prices stored and constantly updated - private volatile ConcurrentHashMap pricesCache = new ConcurrentHashMap<>(); - - public static PricesCacheStore getInstance() { - return INSTANCE; - } - + + // will contain Symbols and prices stored and constantly updated + private volatile ConcurrentHashMap pricesCache = new ConcurrentHashMap<>(); + public void addPrice(String symbol, Double price) { - pricesCache.put(symbol, price); - lastSymboleUpdated = symbol; + if (symbol != null && price != null) { + pricesCache.put(symbol, price); + lastSymboleUpdated = symbol; + } } - + public Double getPrice(String symbol) { + if (symbol == null) { + return 0d; + } Double price = pricesCache.get(symbol); - return price == null ? 0 : price; + return price == null ? 0d : price; } - + // Keep in mind immutability - return a copy of the object // does not matter if it is static or volatile // or if its threading is safe - public ConcurrentHashMap getAllPrices() { + public ConcurrentHashMap getAllPrices() { return new ConcurrentHashMap(pricesCache); } - + public String getLastSymbolUpdated() { return new String(lastSymboleUpdated); } diff --git a/src/main/java/JSR353/JSON/YahooFinanceStub.java b/src/main/java/JSR353/JSON/YahooFinanceStub.java index 4a42b14..c3f9e25 100644 --- a/src/main/java/JSR353/JSON/YahooFinanceStub.java +++ b/src/main/java/JSR353/JSON/YahooFinanceStub.java @@ -2,9 +2,12 @@ import java.io.File; import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.util.Random; import java.util.concurrent.ConcurrentHashMap; +import java.util.logging.Level; +import java.util.logging.Logger; import javax.json.Json; import javax.json.JsonObject; @@ -15,193 +18,204 @@ public class YahooFinanceStub implements Runnable { - private String[] symbols = new String[] {"GOOG", "MSFT", "YHOO", "IBM"}; - - boolean stopRunning = false; - boolean doNotRunWriteJSONObjectToStreamOnlyOnce = true; // enable this flag to execute this implementation! - boolean doNotRunWriteJSONObjectToConsoleOnlyOnce = true; // enable this flag to execute this implementation! - // enabling the above will prevent other implementations that write to stream/console multiple times from working! - - @Override - public void run() { - // Poll yahoo - generate random price for the moment - // ToDO: <-code to poll Yahoo-> - - // Simulate real-time prices - while( !stopRunning ) { - // Post results into cache store - PricesCacheStore pricesCacheStore = PricesCacheStore.getInstance(); - pricesCacheStore.addPrice(getRandomSymbol(), getRandomPrice()); - - // get the last symbol updated - String symbol = pricesCacheStore.getLastSymbolUpdated(); - Double price = pricesCacheStore.getPrice(symbol); - - // print it to console - // Implementation of JsonGenerator and Json in JSR-353 - // Note that you can write to a stream only once - hence running only once! - writeJSONObjectToConsoleOnlyOnce(symbol, price); - - // Implementation of JsonWriter & JsonReader in JSR-353 - /* - * Writes/Reads the specified JSON object or array to the output source. - * This method needs to be called only once for a reader/writer instance - hence running only once. - */ - writeJSONObjectToStreamOnlyOnce(symbol, price); - - // Implementation of JsonObject & JsonObjectBuilder in JSR-353 - // Returns a Json object with a symbol and price - continuously - System.out.println(getJSONObjectFromValues(symbol, price)); - - // Then pause for a bit...before starting all over again - pauseForABit(1000); - } - } - - public void pauseForABit(int milliSecsToWait) { - try { - Thread.sleep(milliSecsToWait); - - } catch (InterruptedException ex) { - throw new RuntimeException( ex ); - } - } - - /* - * Method to write to console only once. - */ - private void writeJSONObjectToConsoleOnlyOnce(String symbol, Double price) { - if (doNotRunWriteJSONObjectToConsoleOnlyOnce) return; - doNotRunWriteJSONObjectToConsoleOnlyOnce = true; - - pauseForABit(1000); - - JsonGenerator generator = Json.createGenerator(System.out); - generator - .writeStartObject() - .write("symbol", symbol) - .write("price", price) - .writeEnd(); - - generator.close(); - } - - /* - * Method to write to stream only once. - */ - private void writeJSONObjectToStreamOnlyOnce(String symbol, Double price) { - if (doNotRunWriteJSONObjectToStreamOnlyOnce) return; - doNotRunWriteJSONObjectToStreamOnlyOnce = true; - - pauseForABit(1000); - - writeJsonObj(getJSONObjectFromValues(symbol, price)); - readJsonObj(getFileInputStream()); - } - - /* - * Function to return a JsonObject - */ - private JsonObject getJSONObjectFromValues(String symbol, Double price) { - JsonObject value = new JsonObjectBuilder() - .add("symbol", symbol) - .add("price", price) - .build(); - return value; - } - - /* - * Method to get output stream for a file. - */ - public String getRandomSymbol() { - String symbol = ""; - if ((symbols != null) && (symbols.length > 0)) { - Random randSeed = new Random(); - int symbolsUpperLimit= symbols.length; - int randomIndex = Math.abs(randSeed.nextInt()) % symbolsUpperLimit; - symbol = symbols[randomIndex]; - } - return symbol; - } - - /* - * Function to return all prices stored in PricesCachseStore - immutable, singleton - */ - public ConcurrentHashMap getAllPrices() { - return PricesCacheStore.getInstance().getAllPrices(); - } - - public static void main(String[] args) { - - YahooFinanceStub yahooStub = new YahooFinanceStub(); - - Thread yahooPricesThread = new Thread(yahooStub); - yahooPricesThread.start(); - } - - /* - * Function to return a randomized price. - */ - static private Double getRandomPrice() { - Random rand = new Random(); - return rand.nextDouble() * 100; - } - - - private static FileOutputStream outputStream = null; - private static FileInputStream inputStream = null; - - /* - * Method to get output stream for a file. - */ - public static FileOutputStream getFileOutputStream() { - try { - if (outputStream == null) { - outputStream = new FileOutputStream(new File("jsr353.txt")); - } else { - return outputStream; - } - } catch (Exception e) { - e.printStackTrace(); - } - return outputStream; - } - - /* - * Method to get input stream for a file. - */ - public static FileInputStream getFileInputStream() { - try { - if (inputStream == null) { - inputStream = new FileInputStream(new File("jsr353.txt")); - } else { - return inputStream; - } - } catch (Exception e) { - e.printStackTrace(); - } - return inputStream; - } - - /* - * Method to write JsonObj to a given output stream. - */ - public static void writeJsonObj(JsonObject jsonObject) { - JsonWriter jsonWriter = new JsonWriter(getFileOutputStream()); - System.out.println("JSR 353 - Write Object: "); - jsonWriter.writeObject(jsonObject); - System.out.println(jsonObject); - jsonWriter.close(); - } - - /* - * Method to write JsonObj from a given output stream. - */ - public static void readJsonObj(FileInputStream stream) { - JsonReader jsonReader = new JsonReader(stream); - System.out.println("JSR 353 - Read Object: "); - JsonObject jsonObject = jsonReader.readObject(); - System.out.println(jsonObject); - jsonReader.close(); - } -} \ No newline at end of file + static final Logger logger = Logger.getLogger(YahooFinanceStub.class.getName()); + + private String[] symbols = new String[] {"GOOG", "MSFT", "YHOO", "IBM"}; + + boolean stopRunning = false; + boolean doNotRunWriteJSONObjectToStreamOnlyOnce = true; // enable this flag to execute this implementation! + boolean doNotRunWriteJSONObjectToConsoleOnlyOnce = true; // enable this flag to execute this implementation! + // enabling the above will prevent other implementations that write to stream/console multiple times from working! + + @Override + public void run() { + // Poll yahoo - generate random price for the moment + // ToDO: <-code to poll Yahoo-> + + // Simulate real-time prices + while( !stopRunning ) { + // Post results into cache store + PricesCacheStore.INSTANCE.addPrice(getRandomSymbol(), getRandomPrice()); + + // get the last symbol updated + String symbol = PricesCacheStore.INSTANCE.getLastSymbolUpdated(); + Double price = PricesCacheStore.INSTANCE.getPrice(symbol); + + // print it to console + // Implementation of JsonGenerator and Json in JSR-353 + // Note that you can write to a stream only once - hence running only once! + writeJSONObjectToConsoleOnlyOnce(symbol, price); + + // Implementation of JsonWriter & JsonReader in JSR-353 + /* + * Writes/Reads the specified JSON object or array to the output source. + * This method needs to be called only once for a reader/writer instance - hence running only once. + */ + writeJSONObjectToStreamOnlyOnce(symbol, price); + + // Implementation of JsonObject & JsonObjectBuilder in JSR-353 + // Returns a Json object with a symbol and price - continuously + logger.log(Level.INFO, "JsonObject: {0}", getJSONObjectFromValues(symbol, price)); + + // Then pause for a bit...before starting all over again + pauseForABit(1000); + } + } + + public void pauseForABit(int milliSecsToWait) { + try { + Thread.sleep(milliSecsToWait); + + } catch (InterruptedException ex) { + throw new RuntimeException( ex ); + } + } + + /* + * Method to write to console only once. + */ + private void writeJSONObjectToConsoleOnlyOnce(String symbol, Double price) { + if (doNotRunWriteJSONObjectToConsoleOnlyOnce) return; + doNotRunWriteJSONObjectToConsoleOnlyOnce = true; + + pauseForABit(1000); + + JsonGenerator generator = Json.createGenerator(System.out); + generator + .writeStartObject() + .write("symbol", symbol) + .write("price", price) + .writeEnd(); + + generator.close(); + } + + /* + * Method to write to stream only once. + */ + private void writeJSONObjectToStreamOnlyOnce(String symbol, Double price) { + if (doNotRunWriteJSONObjectToStreamOnlyOnce) return; + doNotRunWriteJSONObjectToStreamOnlyOnce = true; + + pauseForABit(1000); + + writeJsonObj(getJSONObjectFromValues(symbol, price)); + readJsonObj(getFileInputStream()); + } + + /* + * Function to return a JsonObject + */ + private JsonObject getJSONObjectFromValues(String symbol, Double price) { + JsonObject value = new JsonObjectBuilder() + .add("symbol", symbol) + .add("price", price) + .build(); + return value; + } + + /* + * Method to get output stream for a file. + */ + public String getRandomSymbol() { + String symbol = ""; + if ((symbols != null) && (symbols.length > 0)) { + Random randSeed = new Random(); + int symbolsUpperLimit= symbols.length; + int randomIndex = Math.abs(randSeed.nextInt()) % symbolsUpperLimit; + symbol = symbols[randomIndex]; + } + return symbol; + } + + /* + * Function to return all prices stored in PricesCachseStore - immutable, singleton + */ + public ConcurrentHashMap getAllPrices() { + return PricesCacheStore.INSTANCE.getAllPrices(); + } + + public static void main(String[] args) { + + // Java 7 SimpleFormatter. + System.setProperty("java.util.logging.SimpleFormatter.format", "%1$tH:%1$tM:%1$tS %5$s%6$s%n"); + + YahooFinanceStub yahooStub = new YahooFinanceStub(); + + Thread yahooPricesThread = new Thread(yahooStub); + yahooPricesThread.start(); + } + + /* + * Function to return a randomized price. + */ + static private Double getRandomPrice() { + return new Random().nextDouble() * 100; + } + + + private static FileOutputStream outputStream = null; + private static FileInputStream inputStream = null; + + /* + * Method to get output stream for a file. + */ + public static FileOutputStream getFileOutputStream() { + if (outputStream == null) { + try { + outputStream = new FileOutputStream(new File("jsr353.txt")); + } catch (FileNotFoundException ex) { + Logger.getLogger(YahooFinanceStub.class.getName()).log(Level.SEVERE, null, ex); + } + } else { + return outputStream; + } + return outputStream; + } + + /* + * Method to get input stream for a file. + */ + public static FileInputStream getFileInputStream() { + + if (inputStream == null) { + try { + inputStream = new FileInputStream(new File("jsr353.txt")); + } catch (FileNotFoundException ex) { + Logger.getLogger(YahooFinanceStub.class.getName()).log(Level.SEVERE, null, ex); + } + } else { + return inputStream; + } + + return inputStream; + } + + /* + * Method to write JsonObj to a given output stream. + */ + public static void writeJsonObj(JsonObject jsonObject) { + JsonWriter jsonWriter = new JsonWriter(getFileOutputStream()); + logger.log(Level.INFO, "JSR 353 - Write Object: "); + + jsonWriter.writeObject(jsonObject); + + logger.log(Level.INFO, "The Object : {0}", jsonObject); + + jsonWriter.close(); + } + + /* + * Method to write JsonObj from a given output stream. + */ + public static void readJsonObj(FileInputStream stream) { + JsonReader jsonReader = new JsonReader(stream); + + logger.log(Level.INFO, "JSR 353 - Read Object: "); + + JsonObject jsonObject = jsonReader.readObject(); + + logger.log(Level.INFO, "The Object : {0}", jsonObject); + jsonReader.close(); + } +} diff --git a/src/test/java/JSR353/JSON/AllTests.java b/src/test/java/JSR353/JSON/AllTests.java new file mode 100644 index 0000000..6bcd648 --- /dev/null +++ b/src/test/java/JSR353/JSON/AllTests.java @@ -0,0 +1,14 @@ +package JSR353.JSON; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +import org.junit.runners.Suite.SuiteClasses; + +@RunWith(value = Suite.class) +@SuiteClasses(value = { + PricesCacheStoreTest.class, + YahooFinanceStubTest.class +}) +public class AllTests { + +} diff --git a/src/test/java/JSR353/JSON/PricesCacheStoreTest.java b/src/test/java/JSR353/JSON/PricesCacheStoreTest.java new file mode 100644 index 0000000..015afb9 --- /dev/null +++ b/src/test/java/JSR353/JSON/PricesCacheStoreTest.java @@ -0,0 +1,53 @@ +package JSR353.JSON; + +import java.util.logging.Level; +import java.util.logging.Logger; + +import org.junit.Before; +import org.junit.Test; + +public class PricesCacheStoreTest { + + static final Logger logger = Logger.getLogger(PricesCacheStoreTest.class.getName()); + + @Before + public void setUp() { + PricesCacheStore.INSTANCE.addPrice("", null); + PricesCacheStore.INSTANCE.addPrice("", 0d); + PricesCacheStore.INSTANCE.addPrice("a", 0d); + PricesCacheStore.INSTANCE.addPrice(null, 0d); + PricesCacheStore.INSTANCE.addPrice("3d", 3d); + } + + @Test + public void getPrice() { + + logger.log(Level.INFO, "The symbol: the price: {0}", PricesCacheStore.INSTANCE.getPrice("")); + + logger.log(Level.INFO, "The symbol: null the price: {0}", PricesCacheStore.INSTANCE.getPrice(null)); + + logger.log(Level.INFO, "The symbol: a the price: {0}", PricesCacheStore.INSTANCE.getPrice("a")); + + logger.log(Level.INFO, "The symbol: 3d the price: {0}", PricesCacheStore.INSTANCE.getPrice("3d")); + } + + @Test + public void getLastSymbolUpdated() { + PricesCacheStore.INSTANCE.addPrice("", null); + logger.log(Level.INFO, "The last symbol updated: {0}", PricesCacheStore.INSTANCE.getLastSymbolUpdated()); + + PricesCacheStore.INSTANCE.addPrice("", 0d); + logger.log(Level.INFO, "The last symbol updated: {0}", PricesCacheStore.INSTANCE.getLastSymbolUpdated()); + + PricesCacheStore.INSTANCE.addPrice("a", 0d); + logger.log(Level.INFO, "The last symbol updated: {0}", PricesCacheStore.INSTANCE.getLastSymbolUpdated()); + + PricesCacheStore.INSTANCE.addPrice(null, 0d); + logger.log(Level.INFO, "The last symbol updated: {0}", PricesCacheStore.INSTANCE.getLastSymbolUpdated()); + + PricesCacheStore.INSTANCE.addPrice("3d", 3d); + logger.log(Level.INFO, "The last symbol updated: {0}", PricesCacheStore.INSTANCE.getLastSymbolUpdated()); + + } + +} diff --git a/src/test/java/JSR353/JSON/YahooFinanceStubTest.java b/src/test/java/JSR353/JSON/YahooFinanceStubTest.java new file mode 100644 index 0000000..f983cdc --- /dev/null +++ b/src/test/java/JSR353/JSON/YahooFinanceStubTest.java @@ -0,0 +1,5 @@ +package JSR353.JSON; + +public class YahooFinanceStubTest { + +} From c116a40a78f31b9cfc3f7b8385ef46887d1f103d Mon Sep 17 00:00:00 2001 From: neomatrix369 Date: Mon, 18 Mar 2013 14:19:54 +0000 Subject: [PATCH 15/24] Fixed issues on pull request #1 via hash e7cccad: volatile, 'e', comments in pom.xml --- .gitignore | 2 ++ .gitignore~ | 6 ++++++ pom.xml | 19 ++++++------------- .../java/JSR353/JSON/PricesCacheStore.java | 8 ++++---- 4 files changed, 18 insertions(+), 17 deletions(-) create mode 100644 .gitignore~ diff --git a/.gitignore b/.gitignore index 0f182a0..622383b 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,5 @@ *.jar *.war *.ear +target/ +bin/ diff --git a/.gitignore~ b/.gitignore~ new file mode 100644 index 0000000..0f182a0 --- /dev/null +++ b/.gitignore~ @@ -0,0 +1,6 @@ +*.class + +# Package Files # +*.jar +*.war +*.ear diff --git a/pom.xml b/pom.xml index 456f688..cf0dbdc 100644 --- a/pom.xml +++ b/pom.xml @@ -28,24 +28,17 @@ - - - junit - junit - 4.11 - test - + + junit + junit + 4.11 + test + org.glassfish javax.json 1.0-b02 - - \ No newline at end of file diff --git a/src/main/java/JSR353/JSON/PricesCacheStore.java b/src/main/java/JSR353/JSON/PricesCacheStore.java index 54850aa..e71f81e 100644 --- a/src/main/java/JSR353/JSON/PricesCacheStore.java +++ b/src/main/java/JSR353/JSON/PricesCacheStore.java @@ -6,15 +6,15 @@ public enum PricesCacheStore { INSTANCE; - private volatile String lastSymboleUpdated; + private volatile String lastSymbolUpdated; // will contain Symbols and prices stored and constantly updated - private volatile ConcurrentHashMap pricesCache = new ConcurrentHashMap<>(); + private ConcurrentHashMap pricesCache = new ConcurrentHashMap<>(); public void addPrice(String symbol, Double price) { if (symbol != null && price != null) { pricesCache.put(symbol, price); - lastSymboleUpdated = symbol; + lastSymbolUpdated = symbol; } } @@ -34,6 +34,6 @@ public ConcurrentHashMap getAllPrices() { } public String getLastSymbolUpdated() { - return new String(lastSymboleUpdated); + return new String(lastSymbolUpdated); } } \ No newline at end of file From ea169a782de84ac8ee46ff838c4270fb316840ad Mon Sep 17 00:00:00 2001 From: neomatrix369 Date: Mon, 18 Mar 2013 14:37:01 +0000 Subject: [PATCH 16/24] Remove overkill immutability step, returns object instead. --- src/main/java/JSR353/JSON/PricesCacheStore.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/main/java/JSR353/JSON/PricesCacheStore.java b/src/main/java/JSR353/JSON/PricesCacheStore.java index e71f81e..378b579 100644 --- a/src/main/java/JSR353/JSON/PricesCacheStore.java +++ b/src/main/java/JSR353/JSON/PricesCacheStore.java @@ -26,11 +26,9 @@ public Double getPrice(String symbol) { return price == null ? 0d : price; } - // Keep in mind immutability - return a copy of the object - // does not matter if it is static or volatile - // or if its threading is safe + // Object is threading is safe so can be returned as it is public ConcurrentHashMap getAllPrices() { - return new ConcurrentHashMap(pricesCache); + return pricesCache; } public String getLastSymbolUpdated() { From ea3f159b296ea9082cc633cfac4b134dbfe07dd5 Mon Sep 17 00:00:00 2001 From: neomatrix369 Date: Mon, 18 Mar 2013 16:11:08 +0000 Subject: [PATCH 17/24] Remove .files from the previous push. --- .gitignore | 8 -------- 1 file changed, 8 deletions(-) delete mode 100644 .gitignore diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 622383b..0000000 --- a/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -*.class - -# Package Files # -*.jar -*.war -*.ear -target/ -bin/ From 94cb9f2668408230d8ca237650fd9b524d791a29 Mon Sep 17 00:00:00 2001 From: neomatrix369 Date: Mon, 18 Mar 2013 16:13:29 +0000 Subject: [PATCH 18/24] Remove ...~ file from the previous push. --- .gitignore~ | 6 ------ 1 file changed, 6 deletions(-) delete mode 100644 .gitignore~ diff --git a/.gitignore~ b/.gitignore~ deleted file mode 100644 index 0f182a0..0000000 --- a/.gitignore~ +++ /dev/null @@ -1,6 +0,0 @@ -*.class - -# Package Files # -*.jar -*.war -*.ear From 571d2ae33eee06e8eee2148630a23e87ea5d3c62 Mon Sep 17 00:00:00 2001 From: neomatrix369 Date: Mon, 18 Mar 2013 16:14:01 +0000 Subject: [PATCH 19/24] Remove .settings from the previous push. From d3685b541c5e2b67bcbbfd9afa1d035fac5c9c23 Mon Sep 17 00:00:00 2001 From: neomatrix369 Date: Mon, 18 Mar 2013 16:37:35 +0000 Subject: [PATCH 20/24] Add updated .gitignore file. --- .gitignore | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b2b9c4d --- /dev/null +++ b/.gitignore @@ -0,0 +1,11 @@ +*.class + +# Package Files # +*.jar +*.war +*.ear + +# Ignore . & ~ files except .gitignore # +*~ +.* +!/.gitignore From d7d06b16711a43d92a444bf984d5f9c4e230b485 Mon Sep 17 00:00:00 2001 From: neomatrix369 Date: Fri, 22 Mar 2013 03:00:16 +0000 Subject: [PATCH 21/24] Merge branch 'master' of github.com:neomatrix369/JSR-353-JSON From 6c725f7e39326df8e821fa77cf2b6da70ad80f99 Mon Sep 17 00:00:00 2001 From: neomatrix369 Date: Fri, 22 Mar 2013 03:07:04 +0000 Subject: [PATCH 22/24] Added target folder to .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index b2b9c4d..d0e63c5 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ *~ .* !/.gitignore +target/ From 53519e5f8cf5bb954fb173cff0dc7a77403e37a1 Mon Sep 17 00:00:00 2001 From: neomatrix369 Date: Fri, 22 Mar 2013 12:41:41 +0000 Subject: [PATCH 23/24] Sync-ed local repo with upstream. To remove 'This pull request cannot be automatically merged.' message --- .classpath | 20 -- .gitignore | 12 - .project | 23 -- .settings/org.eclipse.jdt.core.prefs | 12 - .settings/org.eclipse.m2e.core.prefs | 4 - README.md | 28 --- jsr353.txt | 1 - pom.xml | 44 ---- .../java/JSR353/JSON/PricesCacheStore.java | 37 --- .../java/JSR353/JSON/YahooFinanceStub.java | 221 ------------------ src/test/java/JSR353/JSON/AllTests.java | 14 -- .../JSR353/JSON/PricesCacheStoreTest.java | 53 ----- .../JSR353/JSON/YahooFinanceStubTest.java | 5 - .../JSR353/JSON/PricesCacheStore.class | Bin 1833 -> 0 bytes .../JSR353/JSON/YahooFinanceStub.class | Bin 5877 -> 0 bytes target/classes/META-INF/MANIFEST.MF | 5 - .../YahooFinanceStub/pom.properties | 7 - .../YahooFinanceStub/YahooFinanceStub/pom.xml | 43 ---- target/maven-archiver/pom.properties | 5 - 19 files changed, 534 deletions(-) delete mode 100644 .classpath delete mode 100644 .gitignore delete mode 100644 .project delete mode 100644 .settings/org.eclipse.jdt.core.prefs delete mode 100644 .settings/org.eclipse.m2e.core.prefs delete mode 100644 README.md delete mode 100644 jsr353.txt delete mode 100644 pom.xml delete mode 100644 src/main/java/JSR353/JSON/PricesCacheStore.java delete mode 100644 src/main/java/JSR353/JSON/YahooFinanceStub.java delete mode 100644 src/test/java/JSR353/JSON/AllTests.java delete mode 100644 src/test/java/JSR353/JSON/PricesCacheStoreTest.java delete mode 100644 src/test/java/JSR353/JSON/YahooFinanceStubTest.java delete mode 100644 target/classes/JSR353/JSON/PricesCacheStore.class delete mode 100644 target/classes/JSR353/JSON/YahooFinanceStub.class delete mode 100644 target/classes/META-INF/MANIFEST.MF delete mode 100644 target/classes/META-INF/maven/YahooFinanceStub/YahooFinanceStub/pom.properties delete mode 100644 target/classes/META-INF/maven/YahooFinanceStub/YahooFinanceStub/pom.xml delete mode 100644 target/maven-archiver/pom.properties diff --git a/.classpath b/.classpath deleted file mode 100644 index 2dd4731..0000000 --- a/.classpath +++ /dev/null @@ -1,20 +0,0 @@ - - - - - - - - - - - - - - - - - - - - diff --git a/.gitignore b/.gitignore deleted file mode 100644 index d0e63c5..0000000 --- a/.gitignore +++ /dev/null @@ -1,12 +0,0 @@ -*.class - -# Package Files # -*.jar -*.war -*.ear - -# Ignore . & ~ files except .gitignore # -*~ -.* -!/.gitignore -target/ diff --git a/.project b/.project deleted file mode 100644 index f130ce4..0000000 --- a/.project +++ /dev/null @@ -1,23 +0,0 @@ - - - JSR-353-JSON - - - - - - org.eclipse.jdt.core.javabuilder - - - - - org.eclipse.m2e.core.maven2Builder - - - - - - org.eclipse.jdt.core.javanature - org.eclipse.m2e.core.maven2Nature - - diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs deleted file mode 100644 index 6249222..0000000 --- a/.settings/org.eclipse.jdt.core.prefs +++ /dev/null @@ -1,12 +0,0 @@ -eclipse.preferences.version=1 -org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled -org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 -org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve -org.eclipse.jdt.core.compiler.compliance=1.7 -org.eclipse.jdt.core.compiler.debug.lineNumber=generate -org.eclipse.jdt.core.compiler.debug.localVariable=generate -org.eclipse.jdt.core.compiler.debug.sourceFile=generate -org.eclipse.jdt.core.compiler.problem.assertIdentifier=error -org.eclipse.jdt.core.compiler.problem.enumIdentifier=error -org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning -org.eclipse.jdt.core.compiler.source=1.7 diff --git a/.settings/org.eclipse.m2e.core.prefs b/.settings/org.eclipse.m2e.core.prefs deleted file mode 100644 index f897a7f..0000000 --- a/.settings/org.eclipse.m2e.core.prefs +++ /dev/null @@ -1,4 +0,0 @@ -activeProfiles= -eclipse.preferences.version=1 -resolveWorkspaceProjects=true -version=1 diff --git a/README.md b/README.md deleted file mode 100644 index a7c6301..0000000 --- a/README.md +++ /dev/null @@ -1,28 +0,0 @@ -JSR-353-JSON -============ - -```JSR-353-JSON Processing - artefact, follow-on from the LJC hackday``` - -WebSocket & JSON Hack Day (covering implementation for JSR 356 & JSR 353) - - - [Meetup event](http://www.meetup.com/Londonjavacommunity/events/101954512/)
- - [Recorded presentation](http://skillsmatter.com/podcast/home/websock-json-hack-day)
- - [Slides](http://www.slideshare.net/somaynakhal/wesocket-json-hackday)
- - [David Illsley’s github link](https://github.com/davidillsley/json-workshop)
- - [JSR-353 (JSON)](http://glassfish.java.net/adoptajsr/jsr353.html) / [JSON project page](http://json-processing-spec.java.net/)
- -Yahoo! Finance API resources (JSON) - - http://code.google.com/p/yahoo-finance-managed/wiki/YahooFinanceAPIs - - http://developer.yahoo.com/yql/ - - http://finance.yahoo.com/q?s=GOOG&ql=1 - - http://d.yimg.com/autoc.finance.yahoo.com/autoc?query=yahoo&callback=YAHOO.Finance.SymbolSuggest.ssCallback - - http://www.financialcontent.com/support/documentation/json_quote_api.php - - http://developer.yahoo.com/yql/console/?q=show%20tables&env=store://datatables.org/alltableswithkeys - -

Summary of APIs covered

- javax.json.Json - javax.json.JsonObject - javax.json.JsonObjectBuilder - javax.json.JsonReader - javax.json.JsonWriter - javax.json.stream.JsonGenerator diff --git a/jsr353.txt b/jsr353.txt deleted file mode 100644 index 55cf497..0000000 --- a/jsr353.txt +++ /dev/null @@ -1 +0,0 @@ -{"symbol":"GOOG","price":21.02627903437534} \ No newline at end of file diff --git a/pom.xml b/pom.xml deleted file mode 100644 index cf0dbdc..0000000 --- a/pom.xml +++ /dev/null @@ -1,44 +0,0 @@ - - 4.0.0 - YahooFinanceStub - YahooFinanceStub - 0.0.1-SNAPSHOT - YahooFinanceStub - Stub to generate prices and store in the cache! - - - Java EE 7 - https://maven.java.net/content/groups/promoted/ - - - - - - - org.apache.maven.plugins - maven-compiler-plugin - 2.3.2 - - 1.7 - 1.7 - - - - - - - - junit - junit - 4.11 - test - - - - org.glassfish - javax.json - 1.0-b02 - - - \ No newline at end of file diff --git a/src/main/java/JSR353/JSON/PricesCacheStore.java b/src/main/java/JSR353/JSON/PricesCacheStore.java deleted file mode 100644 index 378b579..0000000 --- a/src/main/java/JSR353/JSON/PricesCacheStore.java +++ /dev/null @@ -1,37 +0,0 @@ -package JSR353.JSON; - -import java.util.concurrent.ConcurrentHashMap; - -public enum PricesCacheStore { - - INSTANCE; - - private volatile String lastSymbolUpdated; - - // will contain Symbols and prices stored and constantly updated - private ConcurrentHashMap pricesCache = new ConcurrentHashMap<>(); - - public void addPrice(String symbol, Double price) { - if (symbol != null && price != null) { - pricesCache.put(symbol, price); - lastSymbolUpdated = symbol; - } - } - - public Double getPrice(String symbol) { - if (symbol == null) { - return 0d; - } - Double price = pricesCache.get(symbol); - return price == null ? 0d : price; - } - - // Object is threading is safe so can be returned as it is - public ConcurrentHashMap getAllPrices() { - return pricesCache; - } - - public String getLastSymbolUpdated() { - return new String(lastSymbolUpdated); - } -} \ No newline at end of file diff --git a/src/main/java/JSR353/JSON/YahooFinanceStub.java b/src/main/java/JSR353/JSON/YahooFinanceStub.java deleted file mode 100644 index c3f9e25..0000000 --- a/src/main/java/JSR353/JSON/YahooFinanceStub.java +++ /dev/null @@ -1,221 +0,0 @@ -package JSR353.JSON; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.util.Random; -import java.util.concurrent.ConcurrentHashMap; -import java.util.logging.Level; -import java.util.logging.Logger; - -import javax.json.Json; -import javax.json.JsonObject; -import javax.json.JsonObjectBuilder; -import javax.json.JsonReader; -import javax.json.JsonWriter; -import javax.json.stream.JsonGenerator; - -public class YahooFinanceStub implements Runnable { - - static final Logger logger = Logger.getLogger(YahooFinanceStub.class.getName()); - - private String[] symbols = new String[] {"GOOG", "MSFT", "YHOO", "IBM"}; - - boolean stopRunning = false; - boolean doNotRunWriteJSONObjectToStreamOnlyOnce = true; // enable this flag to execute this implementation! - boolean doNotRunWriteJSONObjectToConsoleOnlyOnce = true; // enable this flag to execute this implementation! - // enabling the above will prevent other implementations that write to stream/console multiple times from working! - - @Override - public void run() { - // Poll yahoo - generate random price for the moment - // ToDO: <-code to poll Yahoo-> - - // Simulate real-time prices - while( !stopRunning ) { - // Post results into cache store - PricesCacheStore.INSTANCE.addPrice(getRandomSymbol(), getRandomPrice()); - - // get the last symbol updated - String symbol = PricesCacheStore.INSTANCE.getLastSymbolUpdated(); - Double price = PricesCacheStore.INSTANCE.getPrice(symbol); - - // print it to console - // Implementation of JsonGenerator and Json in JSR-353 - // Note that you can write to a stream only once - hence running only once! - writeJSONObjectToConsoleOnlyOnce(symbol, price); - - // Implementation of JsonWriter & JsonReader in JSR-353 - /* - * Writes/Reads the specified JSON object or array to the output source. - * This method needs to be called only once for a reader/writer instance - hence running only once. - */ - writeJSONObjectToStreamOnlyOnce(symbol, price); - - // Implementation of JsonObject & JsonObjectBuilder in JSR-353 - // Returns a Json object with a symbol and price - continuously - logger.log(Level.INFO, "JsonObject: {0}", getJSONObjectFromValues(symbol, price)); - - // Then pause for a bit...before starting all over again - pauseForABit(1000); - } - } - - public void pauseForABit(int milliSecsToWait) { - try { - Thread.sleep(milliSecsToWait); - - } catch (InterruptedException ex) { - throw new RuntimeException( ex ); - } - } - - /* - * Method to write to console only once. - */ - private void writeJSONObjectToConsoleOnlyOnce(String symbol, Double price) { - if (doNotRunWriteJSONObjectToConsoleOnlyOnce) return; - doNotRunWriteJSONObjectToConsoleOnlyOnce = true; - - pauseForABit(1000); - - JsonGenerator generator = Json.createGenerator(System.out); - generator - .writeStartObject() - .write("symbol", symbol) - .write("price", price) - .writeEnd(); - - generator.close(); - } - - /* - * Method to write to stream only once. - */ - private void writeJSONObjectToStreamOnlyOnce(String symbol, Double price) { - if (doNotRunWriteJSONObjectToStreamOnlyOnce) return; - doNotRunWriteJSONObjectToStreamOnlyOnce = true; - - pauseForABit(1000); - - writeJsonObj(getJSONObjectFromValues(symbol, price)); - readJsonObj(getFileInputStream()); - } - - /* - * Function to return a JsonObject - */ - private JsonObject getJSONObjectFromValues(String symbol, Double price) { - JsonObject value = new JsonObjectBuilder() - .add("symbol", symbol) - .add("price", price) - .build(); - return value; - } - - /* - * Method to get output stream for a file. - */ - public String getRandomSymbol() { - String symbol = ""; - if ((symbols != null) && (symbols.length > 0)) { - Random randSeed = new Random(); - int symbolsUpperLimit= symbols.length; - int randomIndex = Math.abs(randSeed.nextInt()) % symbolsUpperLimit; - symbol = symbols[randomIndex]; - } - return symbol; - } - - /* - * Function to return all prices stored in PricesCachseStore - immutable, singleton - */ - public ConcurrentHashMap getAllPrices() { - return PricesCacheStore.INSTANCE.getAllPrices(); - } - - public static void main(String[] args) { - - // Java 7 SimpleFormatter. - System.setProperty("java.util.logging.SimpleFormatter.format", "%1$tH:%1$tM:%1$tS %5$s%6$s%n"); - - YahooFinanceStub yahooStub = new YahooFinanceStub(); - - Thread yahooPricesThread = new Thread(yahooStub); - yahooPricesThread.start(); - } - - /* - * Function to return a randomized price. - */ - static private Double getRandomPrice() { - return new Random().nextDouble() * 100; - } - - - private static FileOutputStream outputStream = null; - private static FileInputStream inputStream = null; - - /* - * Method to get output stream for a file. - */ - public static FileOutputStream getFileOutputStream() { - if (outputStream == null) { - try { - outputStream = new FileOutputStream(new File("jsr353.txt")); - } catch (FileNotFoundException ex) { - Logger.getLogger(YahooFinanceStub.class.getName()).log(Level.SEVERE, null, ex); - } - } else { - return outputStream; - } - return outputStream; - } - - /* - * Method to get input stream for a file. - */ - public static FileInputStream getFileInputStream() { - - if (inputStream == null) { - try { - inputStream = new FileInputStream(new File("jsr353.txt")); - } catch (FileNotFoundException ex) { - Logger.getLogger(YahooFinanceStub.class.getName()).log(Level.SEVERE, null, ex); - } - } else { - return inputStream; - } - - return inputStream; - } - - /* - * Method to write JsonObj to a given output stream. - */ - public static void writeJsonObj(JsonObject jsonObject) { - JsonWriter jsonWriter = new JsonWriter(getFileOutputStream()); - logger.log(Level.INFO, "JSR 353 - Write Object: "); - - jsonWriter.writeObject(jsonObject); - - logger.log(Level.INFO, "The Object : {0}", jsonObject); - - jsonWriter.close(); - } - - /* - * Method to write JsonObj from a given output stream. - */ - public static void readJsonObj(FileInputStream stream) { - JsonReader jsonReader = new JsonReader(stream); - - logger.log(Level.INFO, "JSR 353 - Read Object: "); - - JsonObject jsonObject = jsonReader.readObject(); - - logger.log(Level.INFO, "The Object : {0}", jsonObject); - jsonReader.close(); - } -} diff --git a/src/test/java/JSR353/JSON/AllTests.java b/src/test/java/JSR353/JSON/AllTests.java deleted file mode 100644 index 6bcd648..0000000 --- a/src/test/java/JSR353/JSON/AllTests.java +++ /dev/null @@ -1,14 +0,0 @@ -package JSR353.JSON; - -import org.junit.runner.RunWith; -import org.junit.runners.Suite; -import org.junit.runners.Suite.SuiteClasses; - -@RunWith(value = Suite.class) -@SuiteClasses(value = { - PricesCacheStoreTest.class, - YahooFinanceStubTest.class -}) -public class AllTests { - -} diff --git a/src/test/java/JSR353/JSON/PricesCacheStoreTest.java b/src/test/java/JSR353/JSON/PricesCacheStoreTest.java deleted file mode 100644 index 015afb9..0000000 --- a/src/test/java/JSR353/JSON/PricesCacheStoreTest.java +++ /dev/null @@ -1,53 +0,0 @@ -package JSR353.JSON; - -import java.util.logging.Level; -import java.util.logging.Logger; - -import org.junit.Before; -import org.junit.Test; - -public class PricesCacheStoreTest { - - static final Logger logger = Logger.getLogger(PricesCacheStoreTest.class.getName()); - - @Before - public void setUp() { - PricesCacheStore.INSTANCE.addPrice("", null); - PricesCacheStore.INSTANCE.addPrice("", 0d); - PricesCacheStore.INSTANCE.addPrice("a", 0d); - PricesCacheStore.INSTANCE.addPrice(null, 0d); - PricesCacheStore.INSTANCE.addPrice("3d", 3d); - } - - @Test - public void getPrice() { - - logger.log(Level.INFO, "The symbol: the price: {0}", PricesCacheStore.INSTANCE.getPrice("")); - - logger.log(Level.INFO, "The symbol: null the price: {0}", PricesCacheStore.INSTANCE.getPrice(null)); - - logger.log(Level.INFO, "The symbol: a the price: {0}", PricesCacheStore.INSTANCE.getPrice("a")); - - logger.log(Level.INFO, "The symbol: 3d the price: {0}", PricesCacheStore.INSTANCE.getPrice("3d")); - } - - @Test - public void getLastSymbolUpdated() { - PricesCacheStore.INSTANCE.addPrice("", null); - logger.log(Level.INFO, "The last symbol updated: {0}", PricesCacheStore.INSTANCE.getLastSymbolUpdated()); - - PricesCacheStore.INSTANCE.addPrice("", 0d); - logger.log(Level.INFO, "The last symbol updated: {0}", PricesCacheStore.INSTANCE.getLastSymbolUpdated()); - - PricesCacheStore.INSTANCE.addPrice("a", 0d); - logger.log(Level.INFO, "The last symbol updated: {0}", PricesCacheStore.INSTANCE.getLastSymbolUpdated()); - - PricesCacheStore.INSTANCE.addPrice(null, 0d); - logger.log(Level.INFO, "The last symbol updated: {0}", PricesCacheStore.INSTANCE.getLastSymbolUpdated()); - - PricesCacheStore.INSTANCE.addPrice("3d", 3d); - logger.log(Level.INFO, "The last symbol updated: {0}", PricesCacheStore.INSTANCE.getLastSymbolUpdated()); - - } - -} diff --git a/src/test/java/JSR353/JSON/YahooFinanceStubTest.java b/src/test/java/JSR353/JSON/YahooFinanceStubTest.java deleted file mode 100644 index f983cdc..0000000 --- a/src/test/java/JSR353/JSON/YahooFinanceStubTest.java +++ /dev/null @@ -1,5 +0,0 @@ -package JSR353.JSON; - -public class YahooFinanceStubTest { - -} diff --git a/target/classes/JSR353/JSON/PricesCacheStore.class b/target/classes/JSR353/JSON/PricesCacheStore.class deleted file mode 100644 index 5a6ce31937d8428d0dd4cace756dce9af2757fb9..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1833 zcma)6+fvg|6kUfRp(R4W;sq;S!M30k@eZQUdI7OGM(g+((g@L*GHL3JU*xNI9GP)^ zbH;CS9M?ICZ2&Lyp-uKVd#|b|X^Qy~4+d|?({(_1MnEkCubK%jfRVmz3wlx_$NFaE2*l)!*%HUi`2^X0m0 zKU}Msfn8&8v0c;%e8*ei&7QS3yg)9z(hMB8Xw^Nd>HD@96icng+h$|+uDQmbpD)|~1Cy0x$92netETVBcNI(_fqo4K1V)t_|7|se{pgo4Ssh8F zNF`Wx8We8D4(7c^V0spjj^y(Hn@P7>tEpKFoQ!l<^NmKaU5I>DVjtIW0wdI6tw|GH zif4w-n2Kdak^J_M?BJvh4c*kPp`0PGOAUXsGomSQ25d0mI~qrg_{K4!97*@Ijv~&G zF0-j|Jn`@3G@RXJ`LJFJ6F8^iJT6e}nleY#bemM0o+!)HOFAy&ih%Y)#+RN54Ccyz zASSTe2u$l4of|nrC%swMRsC_81&(iLcAz=$?fCKz{VsL8E`2@AQACO9B?$40z zVr!#Y3!V8*B<>>(H#TV*jw^*SZb^vSQlpqXeovDqDWl%>E&HY;S7$Jkrv0~cTIz$N zv{8cpWC;w(rQ_3_^pyJLcJQ=^&mB-s!*294(#OacWAf9p0r7Yp>37)khOZqMUW@qr^Ev$;J)?p1`6=pzqUy^{di)G80k6zaAIceJf*Yuz5~?P(2#hf=96v1B+IF@xFMfCp}c`Mbh`M%!;u(d7=X4bY1heT_a&W!E>`XQ_EW`P# zpx&miI-2T9W!co9j%7^&VfpAwagrHM^d{r`dWlA%;WbBYP9-y`xLFu#q;lEeT-LIn zP-{VnrCPVdILy%}kYY*4(3+y5orNxr^lpg6W64-{qk^lUai@ZNb1G_9Kw*)AIhZR? z)duEazQVk&SkmmtB?ioNpGA$oD-{XHcZSn3dAA$OH)w-CxKL-H7Jd(w77=bS=S6_t zw)XaJ_2OtgbqBZfc~Q@&(6-)QFIEvz=caA}&|ts=uRJvwsDMwN))<&=J*_ohz{h#n zp%{a-wCnYP&n3Ikxug$mSm(j<(^BQkbSz?KHisia%#>8xG;jjmLOKV{Y-ci)l?+k{ zG&B~um_BK}kMK@1a5Cs@9;faIC!?uEP-l@sC8y8GBAIfEfsHs-VQzkkwmf}CM=D3I z>7)(q1~y?cgBXrR?XjyHW*T?2*pnM~%JkC>Y!Qr=bl4TnWG#ee4@bjUGb#hN8R*1z z+UEe<{;Cr8o$JY?Q|L0#E$9M!r&qzW+?K`n8aPu1FPkyAxMV7@1AQI@ixX;JCTk`P zoDC+hi*>^6&F4IUC+(`yk$J0u5Ll$uTvVYnx1>{vo#A-SWCop5VvQv@)J-F;yE3U{ z>vn!vHBKLI$9W!{OS#ZtW`#N+7oz83%5*~#C0}(_OjNx!DV{S>M z+c(9yGIv9#)aiU$D9Y98nNO!h{S0)urDW7O$8xM{WbZ6(vAEL?r^1|IHXPaA9Uj(2 z?n4484+!6(qkTivwy1$&ypzT&$ zRP0eW%E<>!ruK@otQR9ATw=jUXEJN1)45^hYTgj*EfI^yV?i^L=}Yy8g)h~qP-b#& zz_I93lNq`9;JqGPOe^BX5n2P6;C*b4aD`cOtC=*@Vd?|R+(yeF7fG;H61Ihm7JZ!~ z`Kjw?+KR$f zC(U*WIQX$!-gp^a#C-S|uJ+*LMaf_#j)80N3F>E5YnHArz0Y(Aq)!Q?YZXo?0j1-O zJ&TF!4SZU-@#mOG2K6xmuLZZU8xKF7+l#K@{` zX0Qd+krzevdb@$oV?XPM!XkUJW204AJf-^fhVAIoF288tOZYOWk{Ytrgqu?#y{1{z z1#u@wIFhFzLYs22c+^b$a0lw-|uqr5(bMS zSbF}LfpHvRqy_}9LQQc#7bb~1?GbgRY&#?97~#cz3Opvj&Mh(I`Gib>BA%Vh6j&w>n+GjcQ`v_;4wTdtPN1VM0Y7A-ZXdfplK4W zt)dP`#JRveEuK9*Y^J+n36Y@LGAEVjOh!eA4CY6BJZ|YTk2F)6q3uLc$w)4pHj~*_ z?vnQ7ws2;M(o5?V!PsCjoXw@l#F=k;;)a>gb+p(TzmXwr!}kn)6W`)$6X6);y{e(a zk=NRuNN55-7S;X~m4^jP)MHOH;KIvO}MIO5p{Mn#IX7!XbbCa zArV#wS6MxC4Lhi}T!X4oHG;ghZ~|dy9V}?zbu?Z8E>jD*X<@0MmWiJEM8v1SRtea0 zYp=0^T0TnvwK`bmzF;bsj+i3)ggpHQyH?_W2KcT_&txK7r@Wi3;EWDw+~m0rOF}OHvS7&VUmgt2@~;Sn%Ka;Yhq1cJbr_9J?!#!7kCuXmTEYiG zxoI^yzfcQakH&sKQ@fxxUS}=QJgrA4Wl@%c@!Pp z&1*)nwWn!K%NWkkpFM|gmVW3T#W_t2T+fUm+;k5buYcuX!Q;{^UdwwWu#9iZN$Sz` zwi4A?g(Yal3LJ+<-U-*?By8ZA%~-E7Hd#1qj2`xzxPVxzId>34)}4AqhxbL z+Q#wjo|YrHl)C((<|DYm1^-7#??;c|lWrWrbqf9L{*2;*pqR+*#C-;TN$6&1d$E|e zygHmkYIl&xAdW{L@5wuH8u}eXw&z#SuIZEYw`=;!Xkis@#7$hOtcCgS3lU81JZq*!n!I`aG<9 z8XKD!Ab&Jsw_`7Rb) z(!ot#9zvNKG-PkWa}(?S?B+17g<^T2-e8gG$ zGM*2PvbQi;zFJqF47Q>w;b6JU91+# ze$k5>Jb@<(B*3J6iZfiC=f%@}a_c~Ppb2n9MK}lO`XJ#PBAj~&=P==ny$LwHp4o5` zT%v4ID<_mYG_K0Wl(&m|wNT9-Rdcu_sCgp#)cfX9 - 4.0.0 - YahooFinanceStub - YahooFinanceStub - 0.0.1-SNAPSHOT - YahooFinanceStub - Stub to generate prices and store in the cache! - - - Java EE 7 - https://maven.java.net/content/groups/promoted/ - - - - - - - org.apache.maven.plugins - maven-compiler-plugin - 2.3.2 - - 1.7 - 1.7 - - - - - - - - org.glassfish - javax.json - 1.0-b02 - - - - - \ No newline at end of file diff --git a/target/maven-archiver/pom.properties b/target/maven-archiver/pom.properties deleted file mode 100644 index cf0c7bb..0000000 --- a/target/maven-archiver/pom.properties +++ /dev/null @@ -1,5 +0,0 @@ -#Generated by Maven -#Sat Feb 09 14:07:09 GMT 2013 -version=0.0.1-SNAPSHOT -groupId=YahooFinanceStub -artifactId=YahooFinanceStub From 7e316c502c29d8af605d1593715bbb5f204dd210 Mon Sep 17 00:00:00 2001 From: neomatrix369 Date: Fri, 22 Mar 2013 12:45:11 +0000 Subject: [PATCH 24/24] Sync-ed local repo with upstream. To remove 'This pull request cannot be automatically merged.' message --- .gitignore | 12 + README.md | 28 +++ jsr353.txt | 1 + pom.xml | 44 ++++ .../java/JSR353/JSON/PricesCacheStore.java | 37 +++ .../java/JSR353/JSON/YahooFinanceStub.java | 221 ++++++++++++++++++ src/test/java/JSR353/JSON/AllTests.java | 14 ++ .../JSR353/JSON/PricesCacheStoreTest.java | 53 +++++ .../JSR353/JSON/YahooFinanceStubTest.java | 5 + 9 files changed, 415 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 jsr353.txt create mode 100644 pom.xml create mode 100644 src/main/java/JSR353/JSON/PricesCacheStore.java create mode 100644 src/main/java/JSR353/JSON/YahooFinanceStub.java create mode 100644 src/test/java/JSR353/JSON/AllTests.java create mode 100644 src/test/java/JSR353/JSON/PricesCacheStoreTest.java create mode 100644 src/test/java/JSR353/JSON/YahooFinanceStubTest.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..511304f --- /dev/null +++ b/.gitignore @@ -0,0 +1,12 @@ +*.class + +# Package Files # +*.jar +*.war +*.ear + +# Ignore . & ~ files except .gitignore # +*~ +.* +target/ +!/.gitignore diff --git a/README.md b/README.md new file mode 100644 index 0000000..a7c6301 --- /dev/null +++ b/README.md @@ -0,0 +1,28 @@ +JSR-353-JSON +============ + +```JSR-353-JSON Processing - artefact, follow-on from the LJC hackday``` + +WebSocket & JSON Hack Day (covering implementation for JSR 356 & JSR 353) + + - [Meetup event](http://www.meetup.com/Londonjavacommunity/events/101954512/)
+ - [Recorded presentation](http://skillsmatter.com/podcast/home/websock-json-hack-day)
+ - [Slides](http://www.slideshare.net/somaynakhal/wesocket-json-hackday)
+ - [David Illsley’s github link](https://github.com/davidillsley/json-workshop)
+ - [JSR-353 (JSON)](http://glassfish.java.net/adoptajsr/jsr353.html) / [JSON project page](http://json-processing-spec.java.net/)
+ +Yahoo! Finance API resources (JSON) + - http://code.google.com/p/yahoo-finance-managed/wiki/YahooFinanceAPIs + - http://developer.yahoo.com/yql/ + - http://finance.yahoo.com/q?s=GOOG&ql=1 + - http://d.yimg.com/autoc.finance.yahoo.com/autoc?query=yahoo&callback=YAHOO.Finance.SymbolSuggest.ssCallback + - http://www.financialcontent.com/support/documentation/json_quote_api.php + - http://developer.yahoo.com/yql/console/?q=show%20tables&env=store://datatables.org/alltableswithkeys + +

Summary of APIs covered

+ javax.json.Json + javax.json.JsonObject + javax.json.JsonObjectBuilder + javax.json.JsonReader + javax.json.JsonWriter + javax.json.stream.JsonGenerator diff --git a/jsr353.txt b/jsr353.txt new file mode 100644 index 0000000..55cf497 --- /dev/null +++ b/jsr353.txt @@ -0,0 +1 @@ +{"symbol":"GOOG","price":21.02627903437534} \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..cf0dbdc --- /dev/null +++ b/pom.xml @@ -0,0 +1,44 @@ + + 4.0.0 + YahooFinanceStub + YahooFinanceStub + 0.0.1-SNAPSHOT + YahooFinanceStub + Stub to generate prices and store in the cache! + + + Java EE 7 + https://maven.java.net/content/groups/promoted/ + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 2.3.2 + + 1.7 + 1.7 + + + + + + + + junit + junit + 4.11 + test + + + + org.glassfish + javax.json + 1.0-b02 + + + \ No newline at end of file diff --git a/src/main/java/JSR353/JSON/PricesCacheStore.java b/src/main/java/JSR353/JSON/PricesCacheStore.java new file mode 100644 index 0000000..378b579 --- /dev/null +++ b/src/main/java/JSR353/JSON/PricesCacheStore.java @@ -0,0 +1,37 @@ +package JSR353.JSON; + +import java.util.concurrent.ConcurrentHashMap; + +public enum PricesCacheStore { + + INSTANCE; + + private volatile String lastSymbolUpdated; + + // will contain Symbols and prices stored and constantly updated + private ConcurrentHashMap pricesCache = new ConcurrentHashMap<>(); + + public void addPrice(String symbol, Double price) { + if (symbol != null && price != null) { + pricesCache.put(symbol, price); + lastSymbolUpdated = symbol; + } + } + + public Double getPrice(String symbol) { + if (symbol == null) { + return 0d; + } + Double price = pricesCache.get(symbol); + return price == null ? 0d : price; + } + + // Object is threading is safe so can be returned as it is + public ConcurrentHashMap getAllPrices() { + return pricesCache; + } + + public String getLastSymbolUpdated() { + return new String(lastSymbolUpdated); + } +} \ No newline at end of file diff --git a/src/main/java/JSR353/JSON/YahooFinanceStub.java b/src/main/java/JSR353/JSON/YahooFinanceStub.java new file mode 100644 index 0000000..c3f9e25 --- /dev/null +++ b/src/main/java/JSR353/JSON/YahooFinanceStub.java @@ -0,0 +1,221 @@ +package JSR353.JSON; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.util.Random; +import java.util.concurrent.ConcurrentHashMap; +import java.util.logging.Level; +import java.util.logging.Logger; + +import javax.json.Json; +import javax.json.JsonObject; +import javax.json.JsonObjectBuilder; +import javax.json.JsonReader; +import javax.json.JsonWriter; +import javax.json.stream.JsonGenerator; + +public class YahooFinanceStub implements Runnable { + + static final Logger logger = Logger.getLogger(YahooFinanceStub.class.getName()); + + private String[] symbols = new String[] {"GOOG", "MSFT", "YHOO", "IBM"}; + + boolean stopRunning = false; + boolean doNotRunWriteJSONObjectToStreamOnlyOnce = true; // enable this flag to execute this implementation! + boolean doNotRunWriteJSONObjectToConsoleOnlyOnce = true; // enable this flag to execute this implementation! + // enabling the above will prevent other implementations that write to stream/console multiple times from working! + + @Override + public void run() { + // Poll yahoo - generate random price for the moment + // ToDO: <-code to poll Yahoo-> + + // Simulate real-time prices + while( !stopRunning ) { + // Post results into cache store + PricesCacheStore.INSTANCE.addPrice(getRandomSymbol(), getRandomPrice()); + + // get the last symbol updated + String symbol = PricesCacheStore.INSTANCE.getLastSymbolUpdated(); + Double price = PricesCacheStore.INSTANCE.getPrice(symbol); + + // print it to console + // Implementation of JsonGenerator and Json in JSR-353 + // Note that you can write to a stream only once - hence running only once! + writeJSONObjectToConsoleOnlyOnce(symbol, price); + + // Implementation of JsonWriter & JsonReader in JSR-353 + /* + * Writes/Reads the specified JSON object or array to the output source. + * This method needs to be called only once for a reader/writer instance - hence running only once. + */ + writeJSONObjectToStreamOnlyOnce(symbol, price); + + // Implementation of JsonObject & JsonObjectBuilder in JSR-353 + // Returns a Json object with a symbol and price - continuously + logger.log(Level.INFO, "JsonObject: {0}", getJSONObjectFromValues(symbol, price)); + + // Then pause for a bit...before starting all over again + pauseForABit(1000); + } + } + + public void pauseForABit(int milliSecsToWait) { + try { + Thread.sleep(milliSecsToWait); + + } catch (InterruptedException ex) { + throw new RuntimeException( ex ); + } + } + + /* + * Method to write to console only once. + */ + private void writeJSONObjectToConsoleOnlyOnce(String symbol, Double price) { + if (doNotRunWriteJSONObjectToConsoleOnlyOnce) return; + doNotRunWriteJSONObjectToConsoleOnlyOnce = true; + + pauseForABit(1000); + + JsonGenerator generator = Json.createGenerator(System.out); + generator + .writeStartObject() + .write("symbol", symbol) + .write("price", price) + .writeEnd(); + + generator.close(); + } + + /* + * Method to write to stream only once. + */ + private void writeJSONObjectToStreamOnlyOnce(String symbol, Double price) { + if (doNotRunWriteJSONObjectToStreamOnlyOnce) return; + doNotRunWriteJSONObjectToStreamOnlyOnce = true; + + pauseForABit(1000); + + writeJsonObj(getJSONObjectFromValues(symbol, price)); + readJsonObj(getFileInputStream()); + } + + /* + * Function to return a JsonObject + */ + private JsonObject getJSONObjectFromValues(String symbol, Double price) { + JsonObject value = new JsonObjectBuilder() + .add("symbol", symbol) + .add("price", price) + .build(); + return value; + } + + /* + * Method to get output stream for a file. + */ + public String getRandomSymbol() { + String symbol = ""; + if ((symbols != null) && (symbols.length > 0)) { + Random randSeed = new Random(); + int symbolsUpperLimit= symbols.length; + int randomIndex = Math.abs(randSeed.nextInt()) % symbolsUpperLimit; + symbol = symbols[randomIndex]; + } + return symbol; + } + + /* + * Function to return all prices stored in PricesCachseStore - immutable, singleton + */ + public ConcurrentHashMap getAllPrices() { + return PricesCacheStore.INSTANCE.getAllPrices(); + } + + public static void main(String[] args) { + + // Java 7 SimpleFormatter. + System.setProperty("java.util.logging.SimpleFormatter.format", "%1$tH:%1$tM:%1$tS %5$s%6$s%n"); + + YahooFinanceStub yahooStub = new YahooFinanceStub(); + + Thread yahooPricesThread = new Thread(yahooStub); + yahooPricesThread.start(); + } + + /* + * Function to return a randomized price. + */ + static private Double getRandomPrice() { + return new Random().nextDouble() * 100; + } + + + private static FileOutputStream outputStream = null; + private static FileInputStream inputStream = null; + + /* + * Method to get output stream for a file. + */ + public static FileOutputStream getFileOutputStream() { + if (outputStream == null) { + try { + outputStream = new FileOutputStream(new File("jsr353.txt")); + } catch (FileNotFoundException ex) { + Logger.getLogger(YahooFinanceStub.class.getName()).log(Level.SEVERE, null, ex); + } + } else { + return outputStream; + } + return outputStream; + } + + /* + * Method to get input stream for a file. + */ + public static FileInputStream getFileInputStream() { + + if (inputStream == null) { + try { + inputStream = new FileInputStream(new File("jsr353.txt")); + } catch (FileNotFoundException ex) { + Logger.getLogger(YahooFinanceStub.class.getName()).log(Level.SEVERE, null, ex); + } + } else { + return inputStream; + } + + return inputStream; + } + + /* + * Method to write JsonObj to a given output stream. + */ + public static void writeJsonObj(JsonObject jsonObject) { + JsonWriter jsonWriter = new JsonWriter(getFileOutputStream()); + logger.log(Level.INFO, "JSR 353 - Write Object: "); + + jsonWriter.writeObject(jsonObject); + + logger.log(Level.INFO, "The Object : {0}", jsonObject); + + jsonWriter.close(); + } + + /* + * Method to write JsonObj from a given output stream. + */ + public static void readJsonObj(FileInputStream stream) { + JsonReader jsonReader = new JsonReader(stream); + + logger.log(Level.INFO, "JSR 353 - Read Object: "); + + JsonObject jsonObject = jsonReader.readObject(); + + logger.log(Level.INFO, "The Object : {0}", jsonObject); + jsonReader.close(); + } +} diff --git a/src/test/java/JSR353/JSON/AllTests.java b/src/test/java/JSR353/JSON/AllTests.java new file mode 100644 index 0000000..6bcd648 --- /dev/null +++ b/src/test/java/JSR353/JSON/AllTests.java @@ -0,0 +1,14 @@ +package JSR353.JSON; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +import org.junit.runners.Suite.SuiteClasses; + +@RunWith(value = Suite.class) +@SuiteClasses(value = { + PricesCacheStoreTest.class, + YahooFinanceStubTest.class +}) +public class AllTests { + +} diff --git a/src/test/java/JSR353/JSON/PricesCacheStoreTest.java b/src/test/java/JSR353/JSON/PricesCacheStoreTest.java new file mode 100644 index 0000000..015afb9 --- /dev/null +++ b/src/test/java/JSR353/JSON/PricesCacheStoreTest.java @@ -0,0 +1,53 @@ +package JSR353.JSON; + +import java.util.logging.Level; +import java.util.logging.Logger; + +import org.junit.Before; +import org.junit.Test; + +public class PricesCacheStoreTest { + + static final Logger logger = Logger.getLogger(PricesCacheStoreTest.class.getName()); + + @Before + public void setUp() { + PricesCacheStore.INSTANCE.addPrice("", null); + PricesCacheStore.INSTANCE.addPrice("", 0d); + PricesCacheStore.INSTANCE.addPrice("a", 0d); + PricesCacheStore.INSTANCE.addPrice(null, 0d); + PricesCacheStore.INSTANCE.addPrice("3d", 3d); + } + + @Test + public void getPrice() { + + logger.log(Level.INFO, "The symbol: the price: {0}", PricesCacheStore.INSTANCE.getPrice("")); + + logger.log(Level.INFO, "The symbol: null the price: {0}", PricesCacheStore.INSTANCE.getPrice(null)); + + logger.log(Level.INFO, "The symbol: a the price: {0}", PricesCacheStore.INSTANCE.getPrice("a")); + + logger.log(Level.INFO, "The symbol: 3d the price: {0}", PricesCacheStore.INSTANCE.getPrice("3d")); + } + + @Test + public void getLastSymbolUpdated() { + PricesCacheStore.INSTANCE.addPrice("", null); + logger.log(Level.INFO, "The last symbol updated: {0}", PricesCacheStore.INSTANCE.getLastSymbolUpdated()); + + PricesCacheStore.INSTANCE.addPrice("", 0d); + logger.log(Level.INFO, "The last symbol updated: {0}", PricesCacheStore.INSTANCE.getLastSymbolUpdated()); + + PricesCacheStore.INSTANCE.addPrice("a", 0d); + logger.log(Level.INFO, "The last symbol updated: {0}", PricesCacheStore.INSTANCE.getLastSymbolUpdated()); + + PricesCacheStore.INSTANCE.addPrice(null, 0d); + logger.log(Level.INFO, "The last symbol updated: {0}", PricesCacheStore.INSTANCE.getLastSymbolUpdated()); + + PricesCacheStore.INSTANCE.addPrice("3d", 3d); + logger.log(Level.INFO, "The last symbol updated: {0}", PricesCacheStore.INSTANCE.getLastSymbolUpdated()); + + } + +} diff --git a/src/test/java/JSR353/JSON/YahooFinanceStubTest.java b/src/test/java/JSR353/JSON/YahooFinanceStubTest.java new file mode 100644 index 0000000..f983cdc --- /dev/null +++ b/src/test/java/JSR353/JSON/YahooFinanceStubTest.java @@ -0,0 +1,5 @@ +package JSR353.JSON; + +public class YahooFinanceStubTest { + +}