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

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

Go to download

A library for settings and parameterization of objects. Key features are support for data types, help for settings and parameters, annotations for ease of use. Settings and parameters both are Clonable and Serializable. More features for settings such as parsing a settingsfile, being observable, readonliness, caching etc. are available. The library contains javacc generated parsers for syntax support for properties, multi valued properties, parameterized objects and multi valued parameters.

There is a newer version: 14.1
Show newest version
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vectorprint.configuration.parameters;

/*
 * #%L
 * VectorPrintReport
 * %%
 * Copyright (C) 2012 - 2013 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 static com.vectorprint.ArrayHelper.isArrayEqual;
import com.vectorprint.VectorPrintRuntimeException;
import com.vectorprint.configuration.binding.BindingHelper;
import com.vectorprint.configuration.binding.parameters.ParamBindingService;
import com.vectorprint.configuration.binding.parameters.ParameterHelper;
import com.vectorprint.configuration.parameters.annotation.ParamAnnotationProcessorImpl;
import java.io.Serializable;
import java.util.Observable;
import java.util.logging.Logger;

/**
 *
 * @author Eduard Drenth at VectorPrint.nl
 * @param 
 */
public abstract class ParameterImpl extends Observable implements Parameter {

   private static final long serialVersionUID = 1;
   private String key, help;
   private TYPE value;
   private TYPE def;
   private Class declaringClass;
   private Class valueClass;
   protected static final Logger log = Logger.getLogger(ParameterImpl.class.getName());

   /**
    * @param key the value of key
    * @param help the value of help
    */
   public ParameterImpl(String key, String help, Class clazz) {
      this.key = key;
      this.help = help;
      this.valueClass = clazz;
   }

   @Override
   public String getKey() {
      return key;
   }

   /**
    *
    * @return help text appended with default value if it is set
    */
   @Override
   public String getHelp() {
      return def == null ? help : help + " (default: " + ParamBindingService.getInstance().getFactory().getBindingHelper().serializeValue(def) + ")";
   }

   public void setHelp(String help) {
      this.help = help;
   }

   /**
    * NB! used in {@link #equals(java.lang.Object) }
    *
    * @return the TYPE
    */
   @Override
   public TYPE getValue() {
      return value != null ? value : def;
   }

   @Override
   public TYPE getDefault() {
      return def;
   }

   /**
    * Sets the value and notifies Observers
    *
    * @param value the value
    * @return
    */
   @Override
   public Parameter setValue(TYPE value) {
      this.value = value;
      setChanged();
      notifyObservers();
      return this;
   }

   /**
    * Sets the value and notifies Observers
    *
    * @param value the default value
    * @return
    */
   @Override
   public Parameter setDefault(TYPE value) {
      this.def = value;
      setChanged();
      notifyObservers();
      return this;
   }

   /**
    * uses {@link #valueToString(java.io.Serializable) }
    *
    * @return
    */
   @Override
   public final String toString() {
      return getClass().getSimpleName() + "{" + "key=" + key + ", value=" + valueToString(value) + ", def=" + valueToString(def) + ", help=" + help + ", declaringClass=" + declaringClass + '}';
   }

   /**
    * Uses {@link BindingHelper#serializeValue(java.lang.Object) }.
    *
    * @param value
    * @return
    */
   protected String valueToString(TYPE value) {
      StringBuilder sb = new StringBuilder(15);
      if (valueClass.isArray()) {
         sb.append('[');
      }
      sb.append(ParamBindingService.getInstance().getFactory().getBindingHelper().serializeValue(value));
      if (valueClass.isArray()) {
         sb.append(']');
      }
      return sb.toString();
   }

   @Override
   public Parameter clone() {
      try {
         ParameterImpl o = (ParameterImpl) super.clone();
         o.help = help;
         o.key = key;
         o.valueClass = valueClass;
         o.declaringClass = declaringClass;
         return o.setDefault(def).setValue(value);
      } catch (SecurityException | IllegalArgumentException | CloneNotSupportedException ex) {
         throw new VectorPrintRuntimeException(ex);
      }
   }

   @Override
   public Class getDeclaringClass() {
      return declaringClass;
   }

   /**
    * Called from
    * {@link ParamAnnotationProcessorImpl#initParameters(com.vectorprint.configuration.parameters.Parameterizable)}.
    *
    * Call this if you don't use annotations and want to know the declaring class
    *
    * @param declaringClass
    */
   public void setDeclaringClass(Class declaringClass) {
      this.declaringClass = declaringClass;
   }

   @Override
   public Class getValueClass() {
      return valueClass;
   }

   @Override
   public int hashCode() {
      int hash = 5;
      return hash;
   }

   /**
    * NB! calls {@link #getValue() },
    *
    * @see ParameterHelper#isArrayEqual(java.lang.Class, java.lang.Object, java.lang.Object)
    * @param obj
    * @return
    */
   @Override
   public boolean equals(Object obj) {
      if (obj == null) {
         return false;
      }
      if (obj == this) {
         return true;
      }
      if (getClass() != obj.getClass()) {
         return false;
      }
      final ParameterImpl other = (ParameterImpl) obj;
      if ((this.key == null) ? (other.key != null) : !this.key.equals(other.key)) {
         return false;
      }
      if (this.valueClass != other.valueClass && (this.valueClass == null || !this.valueClass.equals(other.valueClass))) {
         return false;
      }
      Object v = (this instanceof PasswordParameter || this instanceof CharPasswordParameter) ? value : getValue();
      Object o = (this instanceof PasswordParameter || this instanceof CharPasswordParameter) ? other.value : other.getValue();
      // compare getValue, not value, if it equals its ok, wether it originates from default or not
      if (!valueClass.isArray()) {
         if (v != o && (v == null || !v.equals(o))) {
            return false;
         }
         if (this.def != other.def && (this.def == null || !this.def.equals(other.def))) {
            return false;
         }
      } else {
         if (v != o && (v == null || !isArrayEqual(v, o))) {
            return false;
         }
         if (this.def != other.def && (this.def == null || !isArrayEqual(this.def, other.def))) {
            return false;
         }
      }
      if (this.declaringClass != other.declaringClass && (this.declaringClass == null || !this.declaringClass.equals(other.declaringClass))) {
         return false;
      }
      if ((this.help == null) ? (other.help != null) : !this.help.equals(other.help)) {
         return false;
      }
      return true;
   }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy