StrengthVarSetImpl.java

  1. // @formatter:off
  2.  /*******************************************************************************
  3.  *
  4.  * This file is part of JMad.
  5.  *
  6.  * Copyright (c) 2008-2011, CERN. All rights reserved.
  7.  *
  8.  * Licensed under the Apache License, Version 2.0 (the "License");
  9.  * you may not use this file except in compliance with the License.
  10.  * You may obtain a copy of the License at
  11.  *
  12.  *     http://www.apache.org/licenses/LICENSE-2.0
  13.  *
  14.  * Unless required by applicable law or agreed to in writing, software
  15.  * distributed under the License is distributed on an "AS IS" BASIS,
  16.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17.  * See the License for the specific language governing permissions and
  18.  * limitations under the License.
  19.  *
  20.  ******************************************************************************/
  21. // @formatter:on

  22. /**
  23.  *
  24.  */
  25. package cern.accsoft.steering.jmad.domain.var.custom;

  26. import java.util.ArrayList;
  27. import java.util.Collection;
  28. import java.util.LinkedHashMap;
  29. import java.util.List;
  30. import java.util.Map;

  31. import cern.accsoft.steering.jmad.domain.knob.strength.Strength;

  32. /**
  33.  * The default implementation of a {@link StrengthVarSet}
  34.  *
  35.  * @author Kajetan Fuchsberger (kajetan.fuchsberger at cern.ch)
  36.  */
  37. public class StrengthVarSetImpl implements StrengthVarSet {

  38.     /** the strengths */
  39.     private final Map<String, Strength> strengths = new LinkedHashMap<String, Strength>();

  40.     /** the variables */
  41.     private final Map<String, CustomVariable> variables = new LinkedHashMap<String, CustomVariable>();

  42.     @Override
  43.     public void addAllStrengths(Collection<Strength> newStrengths) {
  44.         for (Strength strength : newStrengths) {
  45.             String key = unifyKey(strength.getKey());
  46.             remove(key);
  47.             this.strengths.put(key, strength);
  48.         }
  49.     }

  50.     @Override
  51.     public void addAllVariables(Collection<CustomVariable> newVariables) {
  52.         for (CustomVariable variable : newVariables) {
  53.             String key = unifyKey(variable.getKey());
  54.             remove(key);
  55.             this.variables.put(key, variable);
  56.         }
  57.     }

  58.     /**
  59.      * removes element with given key from both maps
  60.      *
  61.      * @param key the key to remove.
  62.      */
  63.     private void remove(String key) {
  64.         this.strengths.remove(key);
  65.         this.variables.remove(key);
  66.     }

  67.     /**
  68.      * just ensures that the keys have a unified appearence.
  69.      *
  70.      * @param key the key to unify
  71.      * @return the unified key
  72.      */
  73.     private static final String unifyKey(String key) {
  74.         return key.trim().toLowerCase();
  75.     }

  76.     @Override
  77.     public List<Strength> getStrengths() {
  78.         return new ArrayList<>(strengths.values());
  79.     }

  80.     @Override
  81.     public List<CustomVariable> getVariables() {
  82.         return new ArrayList<>(variables.values());
  83.     }

  84.     @Override
  85.     public void clear() {
  86.         this.strengths.clear();
  87.         this.variables.clear();
  88.     }

  89.     @Override
  90.     public Strength getStrength(String key) {
  91.         return this.strengths.get(unifyKey(key));
  92.     }

  93. }