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

org.apache.parquet.column.ColumnProperty Maven / Gradle / Ivy

There is a newer version: 3.1.2-23
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.parquet.column;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import org.apache.parquet.hadoop.metadata.ColumnPath;

/**
 * Represents a Parquet property that may have different values for the different columns.
 */
abstract class ColumnProperty {
  private static class DefaultColumnProperty extends ColumnProperty {
    private final T defaultValue;

    private DefaultColumnProperty(T defaultValue) {
      this.defaultValue = defaultValue;
    }

    @Override
    public T getDefaultValue() {
      return defaultValue;
    }

    @Override
    public T getValue(ColumnPath columnPath) {
      return getDefaultValue();
    }

    @Override
    public String toString() {
      return Objects.toString(getDefaultValue());
    }
  }

  private static class MultipleColumnProperty extends DefaultColumnProperty {
    private final Map values;

    private MultipleColumnProperty(T defaultValue, Map values) {
      super(defaultValue);
      assert !values.isEmpty();
      this.values = new HashMap<>(values);
    }

    @Override
    public T getValue(ColumnPath columnPath) {
      T value = values.get(columnPath);
      if (value != null) {
        return value;
      }
      return getDefaultValue();
    }

    @Override
    public String toString() {
      return Objects.toString(getDefaultValue()) + ' ' + values.toString();
    }
  }

  static class Builder {
    private T defaultValue;
    private final Map values = new HashMap<>();

    private Builder() {
    }

    public Builder withDefaultValue(T defaultValue) {
      this.defaultValue = defaultValue;
      return this;
    }

    public Builder withValue(ColumnPath columnPath, T value) {
      values.put(columnPath, value);
      return this;
    }

    public Builder withValue(String columnPath, T value) {
      return withValue(ColumnPath.fromDotString(columnPath), value);
    }

    public Builder withValue(ColumnDescriptor columnDescriptor, T value) {
      return withValue(ColumnPath.get(columnDescriptor.getPath()), value);
    }

    public ColumnProperty build() {
      if (values.isEmpty()) {
        return new DefaultColumnProperty<>(defaultValue);
      } else {
        return new MultipleColumnProperty<>(defaultValue, values);
      }
    }
  }

  public static  Builder builder() {
    return new Builder<>();
  }

  public static  Builder builder(ColumnProperty toCopy) {
    Builder builder = new Builder<>();
    builder.withDefaultValue(((DefaultColumnProperty) toCopy).defaultValue);
    if (toCopy instanceof MultipleColumnProperty) {
      builder.values.putAll(((MultipleColumnProperty) toCopy).values);
    }
    return builder;
  }

  public abstract T getDefaultValue();

  public abstract T getValue(ColumnPath columnPath);

  public T getValue(String columnPath) {
    return getValue(ColumnPath.fromDotString(columnPath));
  }

  public T getValue(ColumnDescriptor columnDescriptor) {
    return getValue(ColumnPath.get(columnDescriptor.getPath()));
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy