
org.apache.jackrabbit.oak.plugins.memory.PropertyBuilder Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.jackrabbit.oak.plugins.memory;
import static org.apache.jackrabbit.guava.common.base.Preconditions.checkArgument;
import static org.apache.jackrabbit.guava.common.base.Preconditions.checkState;
import java.math.BigDecimal;
import java.util.List;
import javax.jcr.PropertyType;
import org.apache.jackrabbit.guava.common.collect.Iterables;
import org.apache.jackrabbit.guava.common.collect.Lists;
import org.apache.jackrabbit.oak.api.Blob;
import org.apache.jackrabbit.oak.api.PropertyState;
import org.apache.jackrabbit.oak.api.Type;
import org.jetbrains.annotations.NotNull;
/**
* {@code PropertyBuilder} for building in memory {@code PropertyState} instances.
* @param
*/
public class PropertyBuilder {
private final Type type;
private String name;
private boolean isArray;
private List values = Lists.newArrayList();
/**
* Create a new instance for building {@code PropertyState} instances
* of the given {@code type}.
* @param type type of the {@code PropertyState} instances to be built.
* @throws IllegalArgumentException if {@code type.isArray()} is {@code true}.
*/
public PropertyBuilder(Type type) {
checkArgument(!type.isArray(), "type must not be array");
this.type = type;
}
/**
* Create a new instance for building scalar {@code PropertyState} instances
* of the given {@code type}.
* @param type type of the {@code PropertyState} instances to be built.
* @return {@code PropertyBuilder} for {@code type}
*/
public static PropertyBuilder scalar(Type type) {
return new PropertyBuilder(type);
}
/**
* Create a new instance for building array {@code PropertyState} instances
* of the given {@code type}.
* @param type type of the {@code PropertyState} instances to be built.
* @return {@code PropertyBuilder} for {@code type}
*/
public static PropertyBuilder array(Type type) {
return new PropertyBuilder(type).setArray();
}
/**
* Create a new instance for building scalar {@code PropertyState} instances
* of the given {@code type}. The builder is initialised with the
* given {@code name}.
* Equivalent to
*
* MemoryPropertyBuilder.create(type).setName(name);
*
* @param type type of the {@code PropertyState} instances to be built.
* @param name initial name
* @return {@code PropertyBuilder} for {@code type}
*/
public static PropertyBuilder scalar(Type type, String name) {
return scalar(type).setName(name);
}
/**
* Create a new instance for building array {@code PropertyState} instances
* of the given {@code type}. The builder is initialised with the
* given {@code name}.
* Equivalent to
*
* MemoryPropertyBuilder.create(type).setName(name).setArray();
*
* @param type type of the {@code PropertyState} instances to be built.
* @param name initial name
* @return {@code PropertyBuilder} for {@code type}
*/
public static PropertyBuilder array(Type type, String name) {
return scalar(type).setName(name).setArray();
}
/**
* Create a new instance for building {@code PropertyState} instances
* of the given {@code type}. The builder is initialised with the name and
* the values of {@code property}.
* Equivalent to
*
* PropertyBuilder.scalar(type).assignFrom(property);
*
*
* @param type type of the {@code PropertyState} instances to be built.
* @param property initial name and values
* @return {@code PropertyBuilder} for {@code type}
*/
public static PropertyBuilder copy(Type type, PropertyState property) {
return scalar(type).assignFrom(property);
}
public String getName() {
return name;
}
public T getValue() {
return values.isEmpty() ? null : values.get(0);
}
@NotNull
public List getValues() {
return Lists.newArrayList(values);
}
public T getValue(int index) {
return values.get(index);
}
public boolean hasValue(Object value) {
return values.contains(value);
}
public int count() {
return values.size();
}
public boolean isArray() {
return isArray;
}
public boolean isEmpty() {
return count() == 0;
}
@SuppressWarnings("unchecked")
@NotNull
public PropertyState getPropertyState() {
checkState(name != null, "Property has no name");
checkState(isArray() || values.size() == 1, "Property has multiple values");
if (values.isEmpty()) {
return EmptyPropertyState.emptyProperty(name, Type.fromTag(type.tag(), true));
} else if (isArray()) {
switch (type.tag()) {
case PropertyType.STRING:
return MultiStringPropertyState.stringProperty(name, (Iterable) values);
case PropertyType.BINARY:
return MultiBinaryPropertyState.binaryPropertyFromBlob(name, (Iterable) values);
case PropertyType.LONG:
return MultiLongPropertyState.createLongProperty(name, (Iterable) values);
case PropertyType.DOUBLE:
return MultiDoublePropertyState.doubleProperty(name, (Iterable) values);
case PropertyType.BOOLEAN:
return MultiBooleanPropertyState.booleanProperty(name, (Iterable) values);
case PropertyType.DECIMAL:
return MultiDecimalPropertyState.decimalProperty(name, (Iterable) values);
default:
return new MultiGenericPropertyState(name, (Iterable) values, Type.fromTag(type.tag(), true));
}
} else {
T value = values.get(0);
switch (type.tag()) {
case PropertyType.STRING:
return StringPropertyState.stringProperty(name, (String) value);
case PropertyType.BINARY:
return BinaryPropertyState.binaryProperty(name, (Blob) value);
case PropertyType.LONG:
return LongPropertyState.createLongProperty(name, (Long) value);
case PropertyType.DOUBLE:
return DoublePropertyState.doubleProperty(name, (Double) value);
case PropertyType.BOOLEAN:
return BooleanPropertyState.booleanProperty(name, (Boolean) value);
case PropertyType.DECIMAL:
return DecimalPropertyState.decimalProperty(name, (BigDecimal) value);
default:
return new GenericPropertyState(name, (String) value, type);
}
}
}
@SuppressWarnings("unchecked")
@NotNull
public PropertyBuilder assignFrom(PropertyState property) {
if (property != null) {
setName(property.getName());
if (property.isArray()) {
isArray = true;
setValues((Iterable) property.getValue(type.getArrayType()));
} else {
isArray = false;
setValue(property.getValue(type));
}
}
return this;
}
@NotNull
public PropertyBuilder setName(String name) {
this.name = name;
return this;
}
@NotNull
public PropertyBuilder setArray() {
isArray = true;
return this;
}
@NotNull
public PropertyBuilder setScalar() {
isArray = false;
return this;
}
@NotNull
public PropertyBuilder setValue(T value) {
values.clear();
values.add(value);
return this;
}
@NotNull
public PropertyBuilder addValue(T value) {
values.add(value);
return this;
}
@NotNull
public PropertyBuilder addValues(Iterable values) {
Iterables.addAll(this.values, values);
return this;
}
@NotNull
public PropertyBuilder setValue(T value, int index) {
values.set(index, value);
return this;
}
@NotNull
public PropertyBuilder setValues(Iterable values) {
this.values = Lists.newArrayList(values);
return this;
}
@NotNull
public PropertyBuilder removeValue(int index) {
values.remove(index);
return this;
}
@NotNull
public PropertyBuilder removeValue(Object value) {
values.remove(value);
return this;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy