public static List<TopDatum> parse(JSONArray a) throws JSONException { List<TopDatum> list = new ArrayList(); DateFormat df = new SimpleDateFormat("HH:mm:ss"); for (int i = 0; i < a.length(); i++) { JSONObject o = a.getJSONObject(i); TopDatum d = new TopDatum(); d.setSymbol(new Symbol(o.getString("symbol"))); d.setName(o.getString("name")); d.setLast(new BigDecimal(o.getString("trade"))); d.setBuy(new BigDecimal(o.getString("buy"))); d.setSell(new BigDecimal(o.getString("sell"))); d.setPrevClose(new BigDecimal(o.getString("settlement"))); d.setChange(new BigDecimal(o.getString("changepercent"))); d.setAmount(new BigDecimal(o.getString("amount"))); d.setOpen(new BigDecimal(o.getString("open"))); try { Date t = df.parse(o.getString("ticktime")); Date date = TimeServer.now(); MarketCalendar mc = Calendars.get(d.getSymbol().getMarket()); if (!mc.isTradingDate(date)) { date = mc.previousTradingDate(date); } else { Date biddingStartTime = mc.getOpenBiddingStartTime(date); if (date.before(biddingStartTime)) { logger.info( "Using the previous date as the current time is before the bidding start time"); date = mc.previousTradingDate(date); } } d.setTime(TimeUtils.merge(date, t)); } catch (ParseException ex) { Logger.getLogger(TopFetcher.class.getName()).log(Level.SEVERE, null, ex); continue; } d.setVolume(o.getLong("volume")); d.setHigh(new BigDecimal(o.getString("high"))); d.setLow(new BigDecimal(o.getString("low"))); d.setMarketCapital(new BigDecimal(o.getString("mktcap")).multiply(new BigDecimal("10000"))); d.setHandOff(new BigDecimal(o.getString("turnoverratio"))); list.add(d); } Collections.sort( list, new Comparator<TopDatum>() { @Override public int compare(TopDatum o1, TopDatum o2) { return -o1.getChange().compareTo(o2.getChange()); } }); return list; }
private Set<Symbol> getAllSymbols() { topFetcher.setTopSize(SYMBOL_SIZE); topFetcher.setTimeout(60); Iterator<TopDatum> iter; try { iter = topFetcher.getData(null); } catch (DataNotAvailableException ex) { throw new RuntimeException(ex); } Set<Symbol> allSymbols = new HashSet(); for (; iter.hasNext(); ) { TopDatum t = iter.next(); allSymbols.add(t.getSymbol()); } if (allSymbols.isEmpty()) { logger.info("No symbols found."); return allSymbols; } // add or not? allSymbols.add(Symbol.SHA_INDEX); allSymbols.add(Symbol.SHE_INDEX); return allSymbols; }