io.helidon.metadata.hson.HsonArray Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of helidon-metadata-hson Show documentation
Show all versions of helidon-metadata-hson Show documentation
Helidon format for metadata (HSON) - a simplified JSON.
The newest version!
/*
* Copyright (c) 2024 Oracle and/or its affiliates.
*
* 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 io.helidon.metadata.hson;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
final class HsonArray implements Hson.Array {
private final List> values;
private HsonArray(List extends Hson.Value>> array) {
this.values = List.copyOf(array);
}
static Hson.Array create() {
return new HsonArray(List.of());
}
static Hson.Array create(List extends Hson.Value>> values) {
return new HsonArray(values);
}
static Hson.Array createStrings(List strings) {
List values = strings.stream()
.map(HsonValues.StringValue::create)
.collect(Collectors.toList());
return new HsonArray(values);
}
static Hson.Array createNumbers(List numbers) {
return new HsonArray(numbers.stream()
.map(HsonValues.NumberValue::create)
.collect(Collectors.toUnmodifiableList()));
}
static Hson.Array createBooleans(List booleans) {
return new HsonArray(booleans.stream()
.map(HsonValues.BooleanValue::create)
.collect(Collectors.toUnmodifiableList()));
}
static Hson.Array create(long... values) {
List collect = LongStream.of(values)
.mapToObj(BigDecimal::new)
.collect(Collectors.toUnmodifiableList());
return Hson.Array.createNumbers(collect);
}
static Hson.Array create(int... values) {
List collect = IntStream.of(values)
.mapToObj(BigDecimal::new)
.collect(Collectors.toUnmodifiableList());
return Hson.Array.createNumbers(collect);
}
static Hson.Array create(double... values) {
List collect = DoubleStream.of(values)
.mapToObj(String::valueOf)
.map(BigDecimal::new)
.collect(Collectors.toUnmodifiableList());
return Hson.Array.createNumbers(collect);
}
static Hson.Array create(float... values) {
List list = new ArrayList<>(values.length);
for (float value : values) {
list.add(new BigDecimal(String.valueOf(value)));
}
return Hson.Array.createNumbers(list);
}
@Override
public List> value() {
return values;
}
@Override
public void write(PrintWriter metaWriter) {
metaWriter.write('[');
for (int i = 0; i < values.size(); i++) {
values.get(i).write(metaWriter);
if (i < (values.size() - 1)) {
metaWriter.write(',');
}
}
metaWriter.write(']');
}
@Override
public Hson.Type type() {
return Hson.Type.ARRAY;
}
@Override
public List getStrings() {
return getTypedList(Hson.Type.STRING);
}
@Override
public List getBooleans() {
return getTypedList(Hson.Type.BOOLEAN);
}
@Override
public List getNumbers() {
return getTypedList(Hson.Type.NUMBER);
}
@Override
public List getStructs() {
return getTypedList(Hson.Type.STRUCT);
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof HsonArray jArray)) {
return false;
}
return Objects.equals(values, jArray.values);
}
@Override
public int hashCode() {
return Objects.hash(values);
}
@Override
public String toString() {
return "["
+ "values=" + values
+ ']';
}
@SuppressWarnings("unchecked")
private List getTypedList(Hson.Type type) {
if (values.isEmpty()) {
return List.of();
}
List list = new ArrayList<>();
for (Hson.Value> value : values) {
if (value.type() == Hson.Type.NULL) {
// null values are ignored
continue;
}
if (value.type() != type) {
throw new HsonException("Requested array of " + type + ", but array element is of type: "
+ value.type());
}
list.add((T) value.value());
}
return List.copyOf(list);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy