Beispiel #1
0
 public void calculateHistoryDistribution(Lottery.Type type) {
   try {
     listUniverse(type);
     List<KeyValuePair> all =
         KeyValuePair.parseArray(
             new JSONArray(
                 SimpleIOUtils.loadContent(
                     new FileInputStream(new File(mRoot, type + "_all_sums")))));
     HistoryDetail detail = HistoryDetail.calculate(new History(mRoot).load(type));
     List<KeyValuePair> historyResult = new ArrayList<>();
     for (int i = 0; i < all.size(); i++) {
       KeyValuePair pair = all.get(i);
       historyResult.add(
           new KeyValuePair(
               pair.getKey(), detail.getTotalCounts()[Integer.parseInt(pair.getKey())] * 10000));
     }
     SimpleIOUtils.saveToFile(
         new File(mRoot, type + "_history_sum"), KeyValuePair.toArray(historyResult).toString());
   } catch (JSONException e) {
     e.printStackTrace();
   } catch (IOException e) {
     e.printStackTrace();
   } catch (DataSource.DataLoadingException e) {
     e.printStackTrace();
   }
 }
Beispiel #2
0
 protected void saveToLocal() {
   final List<HistoryItem> records = mHistoryCache.get(mType);
   final JSONArray jsonArray = new JSONArray();
   for (int i = 0; i < records.size(); i++) {
     jsonArray.put(records.get(i).toJson());
   }
   try {
     SimpleIOUtils.saveToFile(getCacheFile(mType), jsonArray.toString());
   } catch (IOException e) {
     e.printStackTrace();
   }
 }
Beispiel #3
0
 public void historySumDistribution(Lottery.Type type) {
   try {
     List<KeyValuePair> sum = new ArrayList<>();
     List<KeyValuePair> av = new ArrayList<>();
     List<KeyValuePair> av5 = new ArrayList<>();
     List<KeyValuePair> av10 = new ArrayList<>();
     List<HistoryItem> items = new History(mRoot).load(type);
     Collections.reverse(items);
     double total = 0;
     StringBuilder x = new StringBuilder();
     StringBuilder y = new StringBuilder();
     x.append("[");
     y.append("[");
     for (int i = items.size() * 4 / 5; i < items.size(); i++) {
       final HistoryItem item = items.get(i);
       sum.add(new KeyValuePair(item.getDateDisplay(), sum(item)));
       x.append(i);
       y.append(sum(item));
       if (i < items.size() - 1) {
         x.append(",");
         y.append(",");
       }
       total += sum(item);
       av.add(new KeyValuePair(item.getDateDisplay(), (float) (total / sum.size())));
       double total5 = 0;
       for (int j = i; j > i - 5; j--) {
         total5 += sum(items.get(j));
       }
       av5.add(new KeyValuePair(item.getDateDisplay(), (float) (total5 / 5)));
       double total10 = 0;
       for (int j = i; j > i - 10; j--) {
         total10 += sum(items.get(j));
       }
       av10.add(new KeyValuePair(item.getDateDisplay(), (float) (total10 / 10)));
     }
     x.append("];");
     y.append("];");
     SimpleIOUtils.saveToFile(new File(mRoot, type + "disx"), x.toString());
     SimpleIOUtils.saveToFile(new File(mRoot, type + "disy"), y.toString());
     SimpleIOUtils.saveToFile(
         new File(mRoot, type + "_sum_line"), KeyValuePair.toArray(sum).toString());
     SimpleIOUtils.saveToFile(
         new File(mRoot, type + "_sum_line_av"), KeyValuePair.toArray(av).toString());
     SimpleIOUtils.saveToFile(
         new File(mRoot, type + "_sum_line_av5"), KeyValuePair.toArray(av5).toString());
     SimpleIOUtils.saveToFile(
         new File(mRoot, type + "_sum_line_av10"), KeyValuePair.toArray(av10).toString());
   } catch (DataSource.DataLoadingException | IOException e) {
     e.printStackTrace();
   }
 }
Beispiel #4
0
 protected List<HistoryItem> loadFromLocal() throws DataSource.DataLoadingException {
   final File cacheFile = getCacheFile(mType);
   try {
     final String content = SimpleIOUtils.loadContent(new FileInputStream(cacheFile));
     final JSONArray jsonArray = new JSONArray(content);
     final List<HistoryItem> records = new ArrayList<>();
     for (int i = 0; i < jsonArray.length(); i++) {
       final JSONObject json = jsonArray.getJSONObject(i);
       final HistoryItem record = HistoryItem.fromJson(json);
       if (record != null) {
         records.add(record);
       }
     }
     if (records.isEmpty()) {
       return records;
     } else {
       final HistoryItem firstCache = records.get(0);
       final Date date = firstCache.getDate();
       final long deltaTime = System.currentTimeMillis() - date.getTime();
       int dayOfWeek = 3;
       if (mType == Lottery.Type.SSQ) {
         dayOfWeek = 4;
       }
       if (deltaTime < 2 * ONE_DAY
           // 周3是3.
           || (date.getDay() == dayOfWeek && deltaTime < 3 * ONE_DAY)) {
         //                        mDLTs = lotteryRecords;
         mHistoryCache.put(mType, records);
         return mHistoryCache.get(mType);
       }
       final List<HistoryItem> newer = mDataSource.getNewSince(records.get(0));
       records.addAll(0, newer);
       mHistoryCache.put(mType, records);
       saveToLocal();
       return records;
     }
   } catch (IOException | JSONException ignored) {
   }
   return null;
 }
Beispiel #5
0
  public void listUniverse(Lottery.Type type) {
    System.out.println("get all started");
    List<SimpleLottery> all = getAllLotteries(type, true);
    System.out.println("get all finished");
    HashMap<Integer, List<SimpleLottery>> sumDistribution = new HashMap<>();
    for (int i = 0; i < all.size(); i++) {
      SimpleLottery lottery = all.get(i);
      int sum = sum(lottery);
      List<SimpleLottery> value = sumDistribution.get(sum);
      if (value == null) {
        value = new ArrayList<>();
        sumDistribution.put(sum, value);
      }
      value.add(lottery);
    }
    System.out.println("map finished");

    Set<Map.Entry<Integer, List<SimpleLottery>>> entries = sumDistribution.entrySet();
    List<KeyValuePair> pairs = new ArrayList<>();
    for (Map.Entry<Integer, List<SimpleLottery>> entry : entries) {
      pairs.add(new KeyValuePair(String.valueOf(entry.getKey()), entry.getValue().size()));
    }
    Collections.sort(
        pairs,
        new Comparator<KeyValuePair>() {
          @Override
          public int compare(KeyValuePair o1, KeyValuePair o2) {
            return Integer.parseInt(o1.getKey()) - Integer.parseInt(o2.getKey());
          }
        });
    try {
      SimpleIOUtils.saveToFile(
          new File(mRoot, type + "_all_sums"), KeyValuePair.toArray(pairs).toString());
    } catch (IOException e) {
      e.printStackTrace();
    }
  }