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

com.adgear.anoa.read.SetReader Maven / Gradle / Ivy

Go to download

Core classes for Anoa library, which aims to be a safe, convenient and fast record de/serialization wrapper for the Avro, Thrift and Jackson libraries, using the functional idioms of Java 8. The anoa-core module tries to keep upstream dependencies to a minimum.

There is a newer version: 3.1.2
Show newest version
package com.adgear.anoa.read;

import com.adgear.anoa.AnoaJacksonTypeException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;

import java.io.IOException;
import java.util.HashSet;

class SetReader extends AbstractReader> {

  final AbstractReader elementReader;

  SetReader(AbstractReader elementReader) {
    this.elementReader = elementReader;
  }

  @Override
  protected HashSet read(JsonParser jacksonParser) throws IOException {
    if (jacksonParser.getCurrentToken() == JsonToken.START_ARRAY) {
      HashSet result = new HashSet<>();
      doArray(jacksonParser, p -> result.add(elementReader.read(p)));
      return result;
    } else {
      gobbleValue(jacksonParser);
      return null;
    }
  }

  @Override
  protected HashSet readStrict(JsonParser jacksonParser) throws AnoaJacksonTypeException, IOException {
    switch (jacksonParser.getCurrentToken()) {
      case VALUE_NULL:
        return null;
      case START_ARRAY:
        HashSet result = new HashSet<>();
        doArray(jacksonParser, p -> result.add(elementReader.readStrict(p)));
        return result;
      default:
        throw new AnoaJacksonTypeException("Token is not '[': " + jacksonParser.getCurrentToken());
    }
  }
}