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

io.wcm.config.spi.helpers.AbstractParameterProvider Maven / Gradle / Ivy

The newest version!
/*
 * #%L
 * wcm.io
 * %%
 * Copyright (C) 2014 wcm.io
 * %%
 * 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.
 * #L%
 */
package io.wcm.config.spi.helpers;

import io.wcm.config.api.Parameter;
import io.wcm.config.spi.ParameterProvider;

import java.lang.reflect.Field;
import java.util.HashSet;
import java.util.Set;

import org.osgi.annotation.versioning.ConsumerType;

import com.google.common.collect.ImmutableSet;

/**
 * Abstract implementation of {@link ParameterProvider} providing list of parameters either from given
 * parameter set, or from reading all public static fields from a given class definition.
 */
@ConsumerType
public abstract class AbstractParameterProvider implements ParameterProvider {

  private final Set> parameters;

  /**
   * @param parameters Set of parameters for parameter provider
   */
  protected AbstractParameterProvider(Set> parameters) {
    this.parameters = parameters;
  }

  /**
   * @param type Type containing parameter definitions as public static fields.
   */
  protected AbstractParameterProvider(Class type) {
    this(getParametersFromPublicFields(type));
  }

  @Override
  public final Set> getParameters() {
    return parameters;
  }

  /**
   * Get all parameters defined as public static fields in the given type.
   * @param type Type
   * @return Set of parameters
   */
  private static Set> getParametersFromPublicFields(Class type) {
    Set> params = new HashSet<>();
    try {
      Field[] fields = type.getFields();
      for (Field field : fields) {
        if (field.getType().isAssignableFrom(Parameter.class)) {
          params.add((Parameter)field.get(null));
        }
      }
    }
    catch (IllegalArgumentException | IllegalAccessException ex) {
      throw new RuntimeException("Unable to access fields of " + type.getName(), ex);
    }
    return ImmutableSet.copyOf(params);
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy