All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.weaverplatform.protocol.SortedWriteOperationParser Maven / Gradle / Ivy

There is a newer version: 3.27.0
Show newest version
package com.weaverplatform.protocol;


import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.weaverplatform.protocol.model.*;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import java.util.Set;
import java.util.TreeSet;


import static com.weaverplatform.util.SortUtil.*;


/**
 * @author bastbijl
 */
public class SortedWriteOperationParser extends WriteOperationParser {

  public static Character sortChar(String id) {
    if(id == null || id.trim().isEmpty()) {
      return Character.MIN_VALUE;
    }
    return id.charAt(id.length()-1); // use last character
  }

  public static Set startChars(InputStream stream) throws IOException {
    TreeSet set = new TreeSet<>(charComparator);
    JsonReader reader = new JsonReader(new InputStreamReader(stream, "UTF-8"));
    reader.beginArray();

    while(reader.hasNext()) {
      String id = sortKey(gson.fromJson(reader, new TypeToken(){}.getType()));
      Character sc = sortChar(id);
      set.add(sc);
    }
    return set;
  }

  public TreeSet parseNextSorted(InputStream stream, int chunkSize, Character filter) {
    TreeSet set = new TreeSet<>(writeOperationComparator);

    try {
      if (reader == null) {
        reader = new JsonReader(new InputStreamReader(stream, "UTF-8"));
        reader.beginArray();
      }

      while (reader.hasNext() && set.size() < chunkSize) {

        WriteOperation operation = gson.fromJson(reader, new TypeToken(){}.getType());
        if (filter == null || filter.equals(sortChar(sortKey(operation)))) {
          set.add(operation);
        }
      }
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return set;
  }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy