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

org.openlca.io.openepd.EpdQuantity Maven / Gradle / Ivy

There is a newer version: 2.2.1
Show newest version
package org.openlca.io.openepd;

import java.util.Optional;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.openlca.jsonld.Json;

public record EpdQuantity(double amount, String unit) implements Jsonable {

	public static EpdQuantity of(double amount, String unit) {
		return new EpdQuantity(amount, unit);
	}

	public static Optional fromJson(JsonElement elem) {
		if (elem == null || !elem.isJsonObject())
			return Optional.empty();
		var obj = elem.getAsJsonObject();
		var unit = Json.getString(obj, "unit");
		var qty = new EpdQuantity(
			Json.getDouble(obj, "qty", 0),
			unit != null ? unit : ""
		);
		return Optional.of(qty);
	}

	@Override
	public JsonObject toJson() {
		var obj = new JsonObject();
		obj.addProperty("qty", amount);
		obj.addProperty("unit", unit);
		return obj;
	}

	@Override
	public String toString() {
		return amount + " " + unit;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy