View Javadoc

1   /*
2    AbstractOptionExecution.java
3    Creation date : 25/06/2010
4    Copyright © Benjamin Croizet (graffity2199@yahoo.fr)
5   
6    This program is free software; you can redistribute it and/or
7    modify it under the terms of the GNU General Public License
8    or GNU Lesser General Public License as published by the
9    Free Software Foundation; either version 3 of the License,
10   or (at your option) any later version.
11  
12   This program is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16  
17   You should have received copies of the GNU General Public License
18   and GNU Lesser General Public License along with this program;
19   if not, write to the Free Software Foundation, Inc.,
20   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21   http://www.fsf.org/licensing/licenses/gpl.html
22   http://www.gnu.org/licenses/lgpl.html
23   */
24  
25  package net.sourceforge.plantumldependency.commoncli.option.execution;
26  
27  import static java.util.logging.Level.SEVERE;
28  import static java.util.logging.Logger.getLogger;
29  import static net.sourceforge.plantumldependency.common.constants.log.ErrorConstants.UNEXPECTED_ERROR;
30  import static net.sourceforge.plantumldependency.common.utils.comparable.ComparableResult.AFTER;
31  import static net.sourceforge.plantumldependency.common.utils.comparable.ComparableResult.BEFORE;
32  import static net.sourceforge.plantumldependency.common.utils.comparable.ComparableResult.EQUAL;
33  
34  import java.util.logging.Logger;
35  
36  /**
37   * The abstract implementation of the {@link OptionExecution} interface, providing common behaviors.
38   *
39   * @author Benjamin Croizet (<a href="mailto:graffity2199@yahoo.fr>graffity2199@yahoo.fr</a>)
40   * @since 1.3.0
41   * @version 1.3.0
42   */
43  public abstract class AbstractOptionExecution implements OptionExecution {
44  
45      /** The class logger. */
46      private static final transient Logger LOGGER = getLogger(AbstractOptionExecution.class.getName());
47  
48      /** Serial version UID. */
49      private static final long serialVersionUID = -6822376158952497623L;
50  
51      /** The option priority. */
52      private int priority;
53  
54      /**
55       * Default constructor.
56       *
57       * @param optionPriority
58       *            The option priority. <i>Note : the priority must be unique amongst all
59       *            options</i>.
60       * @since 1.3.0
61       */
62      protected AbstractOptionExecution(final int optionPriority) {
63          setPriority(optionPriority);
64      }
65  
66      /**
67       * {@inheritDoc}
68       *
69       * @since 1.3.0
70       */
71      @Override
72      public int compareTo(final OptionExecution o) {
73          final int comparison;
74          if (getPriority() < o.getPriority()) {
75              comparison = BEFORE.getResult();
76          } else if (getPriority() == o.getPriority()) {
77              comparison = EQUAL.getResult();
78          } else {
79              comparison = AFTER.getResult();
80          }
81          return comparison;
82      }
83  
84      /**
85       * {@inheritDoc}
86       *
87       * @since 1.3.0
88       */
89      @Override
90      public OptionExecution deepClone() {
91          OptionExecution o = null;
92  
93          try {
94              o = (AbstractOptionExecution) super.clone();
95          } catch (final CloneNotSupportedException cnse) {
96              LOGGER.log(SEVERE, UNEXPECTED_ERROR, cnse);
97          }
98  
99          return o;
100     }
101 
102     /**
103      * {@inheritDoc}
104      *
105      * @since 1.3.0
106      */
107     @Override
108     public boolean equals(final Object obj) {
109         if (this == obj) {
110             return true;
111         }
112         if (obj == null) {
113             return false;
114         }
115         if (getClass() != obj.getClass()) {
116             return false;
117         }
118         final AbstractOptionExecution other = (AbstractOptionExecution) obj;
119         if (priority != other.priority) {
120             return false;
121         }
122         return true;
123     }
124 
125     /**
126      * {@inheritDoc}
127      *
128      * @since 1.3.0
129      */
130     @Override
131     public int getPriority() {
132         return priority;
133     }
134 
135     /**
136      * {@inheritDoc}
137      *
138      * @since 1.3.0
139      */
140     @Override
141     public int hashCode() {
142         final int prime = 31;
143         int result = 1;
144         result = prime * result + priority;
145         return result;
146     }
147 
148     /**
149      * Sets the value of <code>priority</code>.
150      *
151      * @param value
152      *            the <code>priority</code> to set.
153      * @see #getPriority()
154      * @since 1.3.0
155      */
156     private void setPriority(final int value) {
157         priority = value;
158     }
159 
160     /**
161      * {@inheritDoc}
162      *
163      * @since 1.3.0
164      */
165     @Override
166     public String toString() {
167         return getClass().getSimpleName() + " [priority=" + priority + "]";
168     }
169 }