SequenceDefinitionImpl.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.machine;

  26. import java.util.ArrayList;
  27. import java.util.List;

  28. import com.thoughtworks.xstream.annotations.XStreamAlias;
  29. import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
  30. import com.thoughtworks.xstream.annotations.XStreamConverter;
  31. import com.thoughtworks.xstream.annotations.XStreamOmitField;

  32. import cern.accsoft.steering.jmad.domain.beam.Beam;
  33. import cern.accsoft.steering.jmad.util.xml.converters.NameRefConverter;

  34. /**
  35.  * The default implementation for a sequence-definition. A Sequence definition represents a set of files that define a
  36.  * <a href="http://mad.web.cern.ch/mad/Introduction/sequence.html">SEQUENCE</a> in MadX.
  37.  * <p>
  38.  * This sequence definition also contains several ranges and the beam command associated with the sequence.
  39.  *
  40.  * @author Kajetan Fuchsberger (kajetan.fuchsberger at cern.ch)
  41.  */
  42. @XStreamAlias("sequence")
  43. public class SequenceDefinitionImpl implements SequenceDefinition {

  44.     /** The name of the sequence */
  45.     @XStreamAlias("name")
  46.     @XStreamAsAttribute
  47.     private final String name;

  48.     /** The beam to use for the sequence */
  49.     @XStreamAlias("beam")
  50.     private Beam beam;

  51.     /** The predefined Ranges for this sequence */
  52.     @XStreamAlias("ranges")
  53.     private final List<RangeDefinition> rangeDefinitions = new ArrayList<RangeDefinition>();

  54.     /** the default range definition */
  55.     @XStreamOmitField
  56.     private RangeDefinition defaultRangeDefinition = null;

  57.     @XStreamAlias("default-range")
  58.     @XStreamConverter(NameRefConverter.class)
  59.     private String defaultRangeDefinitionName = null;

  60.     /**
  61.      * default constructor for xstream
  62.      */
  63.     public SequenceDefinitionImpl() {
  64.         this(null, null);
  65.     }

  66.     /**
  67.      * the constructor, which enforces to set a name and a beam.
  68.      *
  69.      * @param name the name of the Sequence
  70.      * @param beam the beam for this sequence. Occasionally this might be <code>null</code>.
  71.      */
  72.     public SequenceDefinitionImpl(String name, Beam beam) {
  73.         this.name = name;
  74.         this.beam = beam;
  75.     }

  76.     public SequenceDefinitionImpl(String name) {
  77.         this.name = name;
  78.     }

  79.     public void setBeam(Beam beam) {
  80.         this.beam = beam;
  81.     }

  82.     @Override
  83.     public Beam getBeam() {
  84.         return this.beam;
  85.     }

  86.     @Override
  87.     public String getName() {
  88.         return this.name;
  89.     }

  90.     @Override
  91.     public List<RangeDefinition> getRangeDefinitions() {
  92.         return this.rangeDefinitions;
  93.     }

  94.     @Override
  95.     public RangeDefinition getDefaultRangeDefinition() {
  96.         return this.defaultRangeDefinition;
  97.     }

  98.     public void setDefaultRangeDefinition(RangeDefinition rangeDefinition) {
  99.         if (rangeDefinition == null) {
  100.             return;
  101.         }

  102.         if (!this.rangeDefinitions.contains(rangeDefinition)) {
  103.             addRangeDefinition(rangeDefinition);
  104.         }

  105.         /* only set it, if the addition worked */
  106.         if (this.rangeDefinitions.contains(rangeDefinition)) {
  107.             this.defaultRangeDefinition = rangeDefinition;
  108.             this.defaultRangeDefinitionName = rangeDefinition.getName();
  109.         }
  110.     }

  111.     /**
  112.      * adds a {@link RangeDefinition} to the sequence
  113.      *
  114.      * @param rangeDefinition the {@link RangeDefinition} to add
  115.      */
  116.     public void addRangeDefinition(RangeDefinition rangeDefinition) {
  117.         this.rangeDefinitions.add(rangeDefinition);
  118.     }

  119.     @Override
  120.     public final RangeDefinition getRangeDefinition(String rangeName) {
  121.         if (rangeName == null) {
  122.             return null;
  123.         }
  124.         for (RangeDefinition rangeDefinition : getRangeDefinitions()) {
  125.             if (rangeName.equals(rangeDefinition.getName())) {
  126.                 return rangeDefinition;
  127.             }
  128.         }
  129.         return null;
  130.     }

  131.     @Override
  132.     public String toString() {
  133.         return getName();
  134.     }

  135.     /**
  136.      * this method is called after deserialization.
  137.      *
  138.      * @return this
  139.      */
  140.     private Object readResolve() {

  141.         /*
  142.          * we have to set the back-references correctly
  143.          */
  144.         for (RangeDefinition rangeDefinition : this.rangeDefinitions) {
  145.             if (rangeDefinition instanceof RangeDefinitionImpl) {
  146.                 ((RangeDefinitionImpl) rangeDefinition).setSequenceDefinition(this);
  147.             }
  148.         }

  149.         /*
  150.          * and set the default range definition if any
  151.          */
  152.         if (this.defaultRangeDefinitionName != null) {
  153.             setDefaultRangeDefinition(getRangeDefinition(this.defaultRangeDefinitionName));
  154.         }

  155.         return this;
  156.     }

  157.     /* NOTE: to avoid issues with missing hashCode() and equals() on JMad objects,
  158.      * and to avoid circular references, the following implementations of hashCode()
  159.      * and equals() DELIBERATELY ONLY OPERATE ON NAMES (Strings)
  160.      *
  161.      * Be careful when changing this behaviour and/or re-generating! */
  162.     @Override
  163.     public int hashCode() {
  164.         final int prime = 31;
  165.         int result = 1;
  166.         result = prime * result + ((name == null) ? 0 : name.hashCode());
  167.         result = prime * result + ((rangeDefinitions == null) ? 0 : rangeDefinitions.hashCode());
  168.         return result;
  169.     }

  170.     @Override
  171.     public boolean equals(Object obj) {
  172.         if (this == obj) {
  173.             return true;
  174.         }
  175.         if (obj == null) {
  176.             return false;
  177.         }
  178.         if (!(obj instanceof SequenceDefinitionImpl)) {
  179.             return false;
  180.         }
  181.         SequenceDefinitionImpl other = (SequenceDefinitionImpl) obj;
  182.         if (name == null) {
  183.             if (other.name != null) {
  184.                 return false;
  185.             }
  186.         } else if (!name.equals(other.name)) {
  187.             return false;
  188.         }
  189.         if (rangeDefinitions == null) {
  190.             if (other.rangeDefinitions != null) {
  191.                 return false;
  192.             }
  193.         } else if (!rangeDefinitions.equals(other.rangeDefinitions)) {
  194.             return false;
  195.         }
  196.         return true;
  197.     }

  198. }