Java tutorial
/* * Copyright 2014 Algodefu * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.algodefu.yeti.web; import com.algodefu.yeti.data.Cluster; import com.algodefu.yeti.data.ClusterRow; import com.algodefu.yeti.data.Deal; import com.google.gson.TypeAdapter; import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonWriter; import java.io.IOException; import java.time.ZoneOffset; import java.util.List; import java.util.Map; /** * Custom type adapter for the class Cluster. * Used by Gson to convert cluster data into Json */ public class ClusterTypeAdapter extends TypeAdapter<Cluster> { @Override public void write(JsonWriter out, Cluster cluster) throws IOException { out.beginArray(); out.value(cluster.getCloseDateTime().toInstant(ZoneOffset.UTC).toEpochMilli()); out.value(cluster.getOpen()); out.value(cluster.getHigh()); out.value(cluster.getLow()); out.value(cluster.getClose()); out.value(cluster.getVolume()); // write clusterRows out.beginArray(); for (Map.Entry<Double, ClusterRow> entry : cluster.getClusterRows().entrySet()) { out.beginArray(); out.value(entry.getKey()); out.value(entry.getValue().getBidVolume()); out.value(entry.getValue().getAskVolume()); out.value(entry.getValue().getNumTicks()); out.endArray(); } out.endArray(); // write deals writeDeals(out, cluster.getInDeals()); writeDeals(out, cluster.getOutDeals()); out.endArray(); } private void writeDeals(JsonWriter out, List<Deal> deals) throws IOException { out.beginArray(); if (deals != null) { for (Deal deal : deals) { out.beginArray(); out.value(deal.getEntry().toString()); out.value(deal.getTradeID()); out.value(deal.getType().toString()); out.value(deal.getVolume()); out.value(deal.getPrice()); out.value(deal.getDateTime().toInstant(ZoneOffset.UTC).toEpochMilli()); out.endArray(); } } out.endArray(); } @Override public Cluster read(JsonReader in) throws IOException { return null; } }