<commit_before>/*
 * 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 java.math.BigDecimal;
import java.util.List;

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

import com.google.common.collect.Lists;
import org.apache.jackrabbit.mk.api.MicroKernel;
import org.apache.jackrabbit.mk.json.JsopReader;
import org.apache.jackrabbit.oak.api.Blob;
import org.apache.jackrabbit.oak.api.CoreValue;
import org.apache.jackrabbit.oak.api.PropertyState;
import org.apache.jackrabbit.oak.api.Type;
import org.apache.jackrabbit.oak.kernel.TypeCodes;
import org.apache.jackrabbit.oak.kernel.KernelBlob;

import static org.apache.jackrabbit.oak.api.Type.DATE;
import static org.apache.jackrabbit.oak.api.Type.DATES;
import static org.apache.jackrabbit.oak.api.Type.NAME;
import static org.apache.jackrabbit.oak.api.Type.NAMES;
import static org.apache.jackrabbit.oak.api.Type.PATH;
import static org.apache.jackrabbit.oak.api.Type.PATHS;
import static org.apache.jackrabbit.oak.api.Type.REFERENCE;
import static org.apache.jackrabbit.oak.api.Type.REFERENCES;
import static org.apache.jackrabbit.oak.api.Type.STRINGS;
import static org.apache.jackrabbit.oak.api.Type.URI;
import static org.apache.jackrabbit.oak.api.Type.URIS;
import static org.apache.jackrabbit.oak.api.Type.WEAKREFERENCE;
import static org.apache.jackrabbit.oak.api.Type.WEAKREFERENCES;

public final class PropertyStates {
    private PropertyStates() {}

    @Nonnull
    public static PropertyState createProperty(String name, CoreValue value) {
        int type = value.getType();
        if (PropertyType.BINARY == type) {
           return binaryProperty(name, new ValueBasedBlob(value));
        }
        else {
            return createProperty(name, value.getString(), value.getType());
        }
    }

    @Nonnull
    public static PropertyState createProperty(String name, List<CoreValue> values) {
        if (values.isEmpty()) {
            return emptyProperty(name, STRINGS);
        }

        int type = values.get(0).getType();
        switch (type) {
            case PropertyType.STRING:
                List<String> strings = Lists.newArrayList();
                for (CoreValue cv : values) {
                    strings.add(cv.getString());
                }
                return stringProperty(name, strings);
            case PropertyType.BINARY:
                List<Blob> blobs = Lists.newArrayList();
                for (CoreValue cv : values) {
                    blobs.add(new ValueBasedBlob(cv));
                }
                return binaryPropertyFromBlob(name, blobs);
            case PropertyType.LONG:
                List<Long> longs = Lists.newArrayList();
                for (CoreValue cv : values) {
                    longs.add(cv.getLong());
                }
                return longProperty(name, longs);
            case PropertyType.DOUBLE:
                List<Double> doubles = Lists.newArrayList();
                for (CoreValue cv : values) {
                    doubles.add(cv.getDouble());
                }
                return doubleProperty(name, doubles);
            case PropertyType.BOOLEAN:
                List<Boolean> booleans = Lists.newArrayList();
                for (CoreValue cv : values) {
                    booleans.add(cv.getBoolean());
                }
                return booleanProperty(name, booleans);
            case PropertyType.DECIMAL:
                List<BigDecimal> decimals = Lists.newArrayList();
                for (CoreValue cv : values) {
                    decimals.add(cv.getDecimal());
                }
                return decimalProperty(name, decimals);
            default:
                List<String> vals = Lists.newArrayList();
                for (CoreValue cv : values) {
                    vals.add(cv.getString());
                }
                return new GenericsPropertyState(name, vals, Type.fromTag(type, true));
        }
    }

    public static PropertyState createProperty(String name, String value, int type) {
        switch (type) {
            case PropertyType.STRING:
                return new StringPropertyState(name, value);
            case PropertyType.BINARY:
                return new BinaryPropertyState(name, new StringBasedBlob(value));
            case PropertyType.LONG:
                return new LongPropertyState(name, SinglePropertyState.getLong(value));
            case PropertyType.DOUBLE:
                return new DoublePropertyState(name, StringPropertyState.getDouble(value));
            case PropertyType.BOOLEAN:
                return new BooleanPropertyState(name, StringPropertyState.getBoolean(value));
            case PropertyType.DECIMAL:
                return new DecimalPropertyState(name, StringPropertyState.getDecimal(value));
            default:
                return new GenericPropertyState(name, value, Type.fromTag(type, false));
        }
    }
<commit_msg>OAK-350: Unify PropertyState and CoreValue - add @Nonnull<commit_after>/*
 * 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 java.math.BigDecimal;
import java.util.List;

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

import com.google.common.collect.Lists;
import org.apache.jackrabbit.mk.api.MicroKernel;
import org.apache.jackrabbit.mk.json.JsopReader;
import org.apache.jackrabbit.oak.api.Blob;
import org.apache.jackrabbit.oak.api.CoreValue;
import org.apache.jackrabbit.oak.api.PropertyState;
import org.apache.jackrabbit.oak.api.Type;
import org.apache.jackrabbit.oak.kernel.TypeCodes;
import org.apache.jackrabbit.oak.kernel.KernelBlob;

import static org.apache.jackrabbit.oak.api.Type.DATE;
import static org.apache.jackrabbit.oak.api.Type.DATES;
import static org.apache.jackrabbit.oak.api.Type.NAME;
import static org.apache.jackrabbit.oak.api.Type.NAMES;
import static org.apache.jackrabbit.oak.api.Type.PATH;
import static org.apache.jackrabbit.oak.api.Type.PATHS;
import static org.apache.jackrabbit.oak.api.Type.REFERENCE;
import static org.apache.jackrabbit.oak.api.Type.REFERENCES;
import static org.apache.jackrabbit.oak.api.Type.STRINGS;
import static org.apache.jackrabbit.oak.api.Type.URI;
import static org.apache.jackrabbit.oak.api.Type.URIS;
import static org.apache.jackrabbit.oak.api.Type.WEAKREFERENCE;
import static org.apache.jackrabbit.oak.api.Type.WEAKREFERENCES;

public final class PropertyStates {
    private PropertyStates() {}

    @Nonnull
    public static PropertyState createProperty(String name, CoreValue value) {
        int type = value.getType();
        if (PropertyType.BINARY == type) {
           return binaryProperty(name, new ValueBasedBlob(value));
        }
        else {
            return createProperty(name, value.getString(), value.getType());
        }
    }

    @Nonnull
    public static PropertyState createProperty(String name, List<CoreValue> values) {
        if (values.isEmpty()) {
            return emptyProperty(name, STRINGS);
        }

        int type = values.get(0).getType();
        switch (type) {
            case PropertyType.STRING:
                List<String> strings = Lists.newArrayList();
                for (CoreValue cv : values) {
                    strings.add(cv.getString());
                }
                return stringProperty(name, strings);
            case PropertyType.BINARY:
                List<Blob> blobs = Lists.newArrayList();
                for (CoreValue cv : values) {
                    blobs.add(new ValueBasedBlob(cv));
                }
                return binaryPropertyFromBlob(name, blobs);
            case PropertyType.LONG:
                List<Long> longs = Lists.newArrayList();
                for (CoreValue cv : values) {
                    longs.add(cv.getLong());
                }
                return longProperty(name, longs);
            case PropertyType.DOUBLE:
                List<Double> doubles = Lists.newArrayList();
                for (CoreValue cv : values) {
                    doubles.add(cv.getDouble());
                }
                return doubleProperty(name, doubles);
            case PropertyType.BOOLEAN:
                List<Boolean> booleans = Lists.newArrayList();
                for (CoreValue cv : values) {
                    booleans.add(cv.getBoolean());
                }
                return booleanProperty(name, booleans);
            case PropertyType.DECIMAL:
                List<BigDecimal> decimals = Lists.newArrayList();
                for (CoreValue cv : values) {
                    decimals.add(cv.getDecimal());
                }
                return decimalProperty(name, decimals);
            default:
                List<String> vals = Lists.newArrayList();
                for (CoreValue cv : values) {
                    vals.add(cv.getString());
                }
                return new GenericsPropertyState(name, vals, Type.fromTag(type, true));
        }
    }

    @Nonnull
    public static PropertyState createProperty(String name, String value, int type) {
        switch (type) {
            case PropertyType.STRING:
                return new StringPropertyState(name, value);
            case PropertyType.BINARY:
                return new BinaryPropertyState(name, new StringBasedBlob(value));
            case PropertyType.LONG:
                return new LongPropertyState(name, SinglePropertyState.getLong(value));
            case PropertyType.DOUBLE:
                return new DoublePropertyState(name, StringPropertyState.getDouble(value));
            case PropertyType.BOOLEAN:
                return new BooleanPropertyState(name, StringPropertyState.getBoolean(value));
            case PropertyType.DECIMAL:
                return new DecimalPropertyState(name, StringPropertyState.getDecimal(value));
            default:
                return new GenericPropertyState(name, value, Type.fromTag(type, false));
        }
    }

{'fraction_non_alphanumeric': 0.09294066695539195, 'fraction_numerical': 0.0011260285838025118, 'mean_word_length': 2.371094890510949, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '537545', 'n_tokens_mistral': 3156, 'n_tokens_neox': 3013, 'n_words': 759}