View Javadoc

1   /*
2    JavaProgramExecutionImpl.java
3    Creation date : 9/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.program.execution.impl;
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.check.ParameterCheckerUtils.checkNull;
31  import static net.sourceforge.plantumldependency.common.utils.comparable.ComparableResult.EQUAL;
32  import static net.sourceforge.plantumldependency.commoncli.constants.log.ErrorConstants.OPTION_EXECUTIONS_NULL_ERROR;
33  
34  import java.util.Set;
35  import java.util.TreeSet;
36  import java.util.logging.Logger;
37  
38  import net.sourceforge.plantumldependency.commoncli.exception.CommandLineException;
39  import net.sourceforge.plantumldependency.commoncli.option.execution.OptionExecution;
40  import net.sourceforge.plantumldependency.commoncli.program.execution.JavaProgramExecution;
41  
42  /**
43   * The default {@link JavaProgramExecution} implementation.
44   *
45   * @author Benjamin Croizet (<a href="mailto:graffity2199@yahoo.fr>graffity2199@yahoo.fr</a>)
46   * @since 1.3.0
47   * @version 1.3.0
48   */
49  public class JavaProgramExecutionImpl implements JavaProgramExecution {
50  
51      /** Serial version UID. */
52      private static final long serialVersionUID = 8850565339062462441L;
53  
54      /** The class logger. */
55      private static final transient Logger LOGGER = getLogger(JavaProgramExecutionImpl.class.getName());
56  
57      /** The {@link Set} of all option executions. */
58      private Set < OptionExecution > optionExecutions;
59  
60      /**
61       * Default constructor.
62       *
63       * @param executions
64       *            the {@link Set} of all option executions, mustn't be <code>null</code>.
65       * @since 1.3.0
66       */
67      public JavaProgramExecutionImpl(final Set < OptionExecution > executions) {
68          setOptionExecutions(executions);
69      }
70  
71      /**
72       * {@inheritDoc}
73       *
74       * @since 1.3.0
75       */
76      @Override
77      public int compareTo(final JavaProgramExecution o) {
78          return EQUAL.getResult();
79      }
80  
81      /**
82       * {@inheritDoc}
83       *
84       * @since 1.3.0
85       */
86      @Override
87      public JavaProgramExecution deepClone() {
88          JavaProgramExecutionImpl j = null;
89  
90          try {
91              j = (JavaProgramExecutionImpl) super.clone();
92              j.optionExecutions = new TreeSet < OptionExecution >();
93              for (final OptionExecution optionExecution : getOptionExecutions()) {
94                  j.optionExecutions.add(optionExecution.deepClone());
95              }
96          } catch (final CloneNotSupportedException cnse) {
97              LOGGER.log(SEVERE, UNEXPECTED_ERROR, cnse);
98          }
99  
100         return j;
101     }
102 
103     /**
104      * {@inheritDoc}
105      *
106      * @since 1.3.0
107      */
108     @Override
109     public boolean equals(final Object obj) {
110         if (this == obj) {
111             return true;
112         }
113         if (obj == null) {
114             return false;
115         }
116         if (getClass() != obj.getClass()) {
117             return false;
118         }
119         final JavaProgramExecutionImpl other = (JavaProgramExecutionImpl) obj;
120         if (optionExecutions == null) {
121             if (other.optionExecutions != null) {
122                 return false;
123             }
124         } else if (!optionExecutions.equals(other.optionExecutions)) {
125             return false;
126         }
127         return true;
128     }
129 
130     /**
131      * {@inheritDoc}
132      *
133      * @since 1.3.0
134      */
135     @Override
136     public void execute() throws CommandLineException {
137         for (final OptionExecution optionExecution : getOptionExecutions()) {
138             optionExecution.execute();
139         }
140     }
141 
142     /**
143      * {@inheritDoc}
144      *
145      * @since 1.3.0
146      */
147     @Override
148     public Set < OptionExecution > getOptionExecutions() {
149         return optionExecutions;
150     }
151 
152     /**
153      * {@inheritDoc}
154      *
155      * @since 1.3.0
156      */
157     @Override
158     public int hashCode() {
159         final int prime = 31;
160         int result = 1;
161         result = prime * result + ((optionExecutions == null) ? 0 : optionExecutions.hashCode());
162         return result;
163     }
164 
165     /**
166      * Sets the value of <code>optionExecutions</code>.
167      *
168      * @param value
169      *            the <code>optionExecutions</code> to set, can be <code>null</code>.
170      * @see #getOptionExecutions()
171      * @since 1.3.0
172      */
173     private void setOptionExecutions(final Set < OptionExecution > value) {
174         checkNull(value, OPTION_EXECUTIONS_NULL_ERROR);
175 
176         optionExecutions = value;
177     }
178 
179     /**
180      * {@inheritDoc}
181      *
182      * @since 1.3.0
183      */
184     @Override
185     public String toString() {
186         return getClass().getSimpleName() + " [optionExecutions=" + optionExecutions + "]";
187     }
188 }