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

com.dslplatform.json.runtime.EnumDescription Maven / Gradle / Ivy

There is a newer version: 1.10.0
Show newest version
package com.dslplatform.json.runtime;

import com.dslplatform.json.JsonReader;
import com.dslplatform.json.JsonWriter;

import java.io.IOException;

public final class EnumDescription> implements JsonWriter.WriteObject, JsonReader.ReadObject {

	private final Class manifest;
	private final DecodePropertyInfo[] decoders;

	public EnumDescription(
			final Class manifest,
			final T[] values) {
		if (manifest == null) throw new IllegalArgumentException("manifest can't be null");
		if (values == null) throw new IllegalArgumentException("values can't be null");
		this.manifest = manifest;
		final DecodePropertyInfo[] tmp = new DecodePropertyInfo[values.length];
		for (int i = 0; i < values.length; i++) {
			T value = values[i];
			tmp[i] = new DecodePropertyInfo<>(value.name(), false, false, i, false, value);
		}
		this.decoders = DecodePropertyInfo.prepare(tmp);
	}

	@Override
	public void write(final JsonWriter writer, final T value) {
		if (value == null) writer.writeNull();
		else writer.writeString(value.name());
	}

	@Override
	public T read(final JsonReader reader) throws IOException {
		if (reader.wasNull()) return null;
		final int hash = reader.calcHash();
		for (final DecodePropertyInfo ri : decoders) {
			if (hash == ri.hash) {
				if (ri.exactName && !reader.wasLastName(ri.nameBytes)) continue;
				return ri.value;
			}
		}
		return Enum.valueOf(manifest, reader.getLastName());
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy