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

com.vectorprint.configuration.parameters.ParameterizableImpl Maven / Gradle / Ivy

Go to download

This project is about configuration of applications and about parameterization of Objects. This library offers annotations (and annotation processors), parsers, typing, observing changes, serialization, cloning and more when working with settings and/or object parameters. Settings and its features can be declared using an xml format. The library offers syntax support for settings and parameters in a loosely coupled manner. You are not restricted to built in syntax, you can provide your own. At runtime this library tracks keys for which a default is used because they are not found in settings. Also it tracks unused keys. You can stack features for settings such as caching, preparing keys and values, readonlyness, threadsafety, helpsupport, reading / parsing from input. You can easily develop your own features for settings.

There is a newer version: 12.2
Show newest version

package com.vectorprint.configuration.parameters;

/*-
 * #%L
 * Config
 * %%
 * Copyright (C) 2015 - 2018 VectorPrint
 * %%
 * 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%
 */

import com.vectorprint.VectorPrintRuntimeException;
import com.vectorprint.configuration.EnhancedMap;
import com.vectorprint.configuration.Settings;
import com.vectorprint.configuration.annotation.SettingsAnnotationProcessor;
import com.vectorprint.configuration.annotation.SettingsAnnotationProcessorImpl;
import com.vectorprint.configuration.annotation.SettingsField;
import com.vectorprint.configuration.binding.parameters.ParameterizableParser;
import com.vectorprint.configuration.decoration.CachingProperties;
import com.vectorprint.configuration.generated.parser.ParameterizableParserImpl;
import com.vectorprint.configuration.parameters.annotation.ParamAnnotationProcessor;
import com.vectorprint.configuration.parameters.annotation.ParamAnnotationProcessorImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
import java.util.Observable;

public class ParameterizableImpl implements Parameterizable {

   public static final ParamAnnotationProcessor paramProcessor = new ParamAnnotationProcessorImpl();
   protected static final Logger logger = LoggerFactory.getLogger(ParameterizableImpl.class.getName());
   private static final SettingsAnnotationProcessor sap = new SettingsAnnotationProcessorImpl();

   private final Map parameters = new HashMap(5) {
      @Override
      public Parameter remove(Object key) {
         return null;
      }

      @Override
      public Parameter put(String key, Parameter value) {
         if (containsKey(key)) {
            throw new VectorPrintRuntimeException(String.format("parameter already known %s: %s", key, get(key)));
         }
         return super.put(key, value);
      }

      @Override
      public void clear() {
      }
   };

   /**
    * will be initialized from parsing {@link ParameterizableParserImpl}
    */
   @SettingsField
   private EnhancedMap settings = new CachingProperties(new Settings(0));

   /**
    * Adds the parameter to this Parameterizable and registers this Parameterizable with the Parameter as Observer.
    * Calls {@link SettingsAnnotationProcessor#initSettings(java.lang.Object, com.vectorprint.configuration.EnhancedMap)
    * }
    * on the parameter class and the parameter object. Sets the {@link Parameter#getDeclaringClass() declaring class}
    *
    * @param parameter
    */
   @Override
   public final void addParameter(Parameter parameter, Class declaringClass) {
      sap.initSettings(parameter.getClass(), settings);
      sap.initSettings(parameter, settings);
      parameters.put(parameter.getKey(), parameter);
      parameter.addObserver(this);
      if (parameter instanceof ParameterImpl) {
         ((ParameterImpl) parameter).setDeclaringClass(declaringClass);
      }
   }

   @Override
   public  Parameter getParameter(String key, Class T) {
      return parameters.get(key);
   }

   @Override
   public Map getParameters() {
      return parameters;
   }

   @Override
   public  TYPE getValue(String key, Class T) {
      if (!parameters.containsKey(key)) {
         throw new VectorPrintRuntimeException(String.format("parameter %s not found in %s", key, getClass()));
      }
      return (TYPE) parameters.get(key).getValue();
   }

   @Override
   public  void setValue(String key, TYPE value) {
      parameters.get(key).setValue(value);
   }

   /**
    *
    * @return
    */
   @Override
   public Parameterizable clone() throws CloneNotSupportedException {
      try {
         Constructor con = getClass().getConstructor();
         ParameterizableImpl pi = (ParameterizableImpl) con.newInstance();
         paramProcessor.initParameters(pi);
         pi.parameters.values().forEach((p) -> p.setValue(getParameters().get(p.getKey()).getValue()));
         return pi;
      } catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
         throw new VectorPrintRuntimeException(ex);
      }
   }

   @Override
   public boolean isParameterSet(String key) {
      return parameters.containsKey(key) && parameters.get(key).getValue() != null;
   }

   /**
    * does nothing
    *
    * @param o
    * @param arg
    */
   @Override
   public void update(Observable o, Object arg) {

   }

   @Override
   public int hashCode() {
      int hash = 7;
      hash = 79 * hash + this.parameters.hashCode();
      return hash;
   }

   @Override
   public boolean equals(Object obj) {
      if (obj == null) {
         return false;
      }
      if (obj == this) {
         return true;
      }
      if (getClass() != obj.getClass()) {
         return false;
      }
      final ParameterizableImpl other = (ParameterizableImpl) obj;
      return !(this.parameters != other.parameters && !this.parameters.equals(other.parameters));
   }

   @Override
   public String toString() {
      return getClass().getSimpleName() + "{" + "parameters=" + parameters + '}';
   }

   /**
    * Returns settings for this Parameterizable.
    *
    * @return
    */
   public EnhancedMap getSettings() {
      return settings;
   }

   /**
    * Calls {@link SettingsAnnotationProcessor#initSettings(java.lang.Object, com.vectorprint.configuration.EnhancedMap)
    * } (only when settings argument is not null) and
    * {@link ParamAnnotationProcessor#initParameters(com.vectorprint.configuration.parameters.Parameterizable) }. This
    * method is meant to be called when this object is not the result of parsing, see {@link ParameterizableParser#parseParameterizable()
    * }.
    *
    * @param settings
    * @throws NoSuchMethodException
    * @throws InstantiationException
    * @throws IllegalAccessException
    * @throws InvocationTargetException
    */
   public void initialize(EnhancedMap settings) throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
      if (settings != null) {
         this.settings = settings;
         sap.initSettings(this, settings);
      }
      paramProcessor.initParameters(this);
   }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy