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

org.apache.jackrabbit.oak.plugins.memory.PropertyBuilder Maven / Gradle / Ivy

There is a newer version: 1.62.0
Show newest version
/*
 * 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 com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;

import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Nonnull;
import javax.jcr.PropertyType;

import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import org.apache.jackrabbit.oak.api.Blob;
import org.apache.jackrabbit.oak.api.PropertyState;
import org.apache.jackrabbit.oak.api.Type;

/**
 * {@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); } @Nonnull 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") @Nonnull 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") @Nonnull 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; } @Nonnull public PropertyBuilder setName(String name) { this.name = name; return this; } @Nonnull public PropertyBuilder setArray() { isArray = true; return this; } @Nonnull public PropertyBuilder setScalar() { isArray = false; return this; } @Nonnull public PropertyBuilder setValue(T value) { values.clear(); values.add(value); return this; } @Nonnull public PropertyBuilder addValue(T value) { values.add(value); return this; } @Nonnull public PropertyBuilder addValues(Iterable values) { Iterables.addAll(this.values, values); return this; } @Nonnull public PropertyBuilder setValue(T value, int index) { values.set(index, value); return this; } @Nonnull public PropertyBuilder setValues(Iterable values) { this.values = Lists.newArrayList(values); return this; } @Nonnull public PropertyBuilder removeValue(int index) { values.remove(index); return this; } @Nonnull public PropertyBuilder removeValue(Object value) { values.remove(value); return this; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy