com.adobe.acs.commons.data.CompositeVariant Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of acs-aem-commons-bundle Show documentation
Show all versions of acs-aem-commons-bundle Show documentation
Main ACS AEM Commons OSGi Bundle. Includes commons utilities.
/*
* ACS AEM Commons
*
* Copyright (C) 2013 - 2023 Adobe
*
* 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 com.adobe.acs.commons.data;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import org.osgi.annotation.versioning.ProviderType;
/**
* Represents a value which could be either a list of variants or a single variant. The idea is that this supports a
* transition from a singular to a multi-value property for easier conversion.
*
* @param Can be of any class supported by Variant, used for direct conversion in getValue, etc.
*/
@ProviderType
public final class CompositeVariant {
private final Class type;
private final List values = new ArrayList<>();
/**
* Create a variant either as a preferred type (set value later with addValue) or
* with an initial value and the preferred type is assumed by the value provided.
* @param initial
*/
public CompositeVariant(T initial) {
if (initial instanceof Class) {
this.type = (Class) initial;
} else {
if (initial instanceof Variant) {
this.type = ((Variant) initial).getBaseType();
} else {
this.type = initial.getClass();
}
addValue(initial);
}
}
public boolean isArray() {
return type.isArray();
}
public Class getSingularType() {
if (isArray()) {
return (Class) type.getComponentType();
} else {
return type;
}
}
public boolean isEmpty() {
return values.isEmpty() || getValue() == null;
}
public final void addValue(Object val) {
if (val instanceof Variant) {
values.add((Variant) val);
} else {
values.add(new Variant(val));
}
}
public T getValue() {
return (T) getValueAs(getSingularType());
}
public U getValueAs(Class otherType) {
return values.isEmpty() ? null : (U) values.get(0).asType(otherType);
}
public List getValues() {
return getValuesAs(getSingularType());
}
public List getValuesAs(Class otherType) {
return values.stream().map(v -> getValueAsType(v, otherType)).collect(Collectors.toList());
}
private U getValueAsType(Variant v, Class type) {
// This shouldn't be necessary but it helps disambiguate a runtime lambda issue
return v.asType(type);
}
@Override
public String toString() {
if (isArray()) {
return getValues().toString();
} else {
return getValueAs(String.class);
}
}
public Object toPropertyValue() {
if (isArray()) {
Object arr = Array.newInstance(getSingularType(), 0);
return getValues().toArray((Object[]) arr);
} else {
return getValue();
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy