JAL-3746 apply copyright to source
[jalview.git] / src / jalview / structure / StructureCommand.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.structure;
22
23 import java.util.ArrayList;
24 import java.util.List;
25
26 public class StructureCommand implements StructureCommandI
27 {
28   private String command;
29
30   private List<String> parameters;
31
32   public StructureCommand(String cmd, String... params)
33   {
34     command = cmd;
35     if (params != null)
36     {
37       for (String p : params)
38       {
39         addParameter(p);
40       }
41     }
42   }
43
44   @Override
45   public void addParameter(String param)
46   {
47     if (parameters == null)
48     {
49       parameters = new ArrayList<>();
50     }
51     parameters.add(param);
52   }
53
54   @Override
55   public String getCommand()
56   {
57     return command;
58   }
59
60   @Override
61   public List<String> getParameters()
62   {
63     return parameters;
64   }
65
66   @Override
67   public boolean hasParameters()
68   {
69     return parameters != null && !parameters.isEmpty();
70   }
71
72   @Override
73   public String toString()
74   {
75     if (!hasParameters())
76     {
77       return command;
78     }
79     StringBuilder sb = new StringBuilder(32);
80     sb.append(command).append("(");
81     boolean first = true;
82     for (String p : parameters)
83     {
84       if (!first)
85       {
86         sb.append(",");
87       }
88       first = false;
89       sb.append(p);
90     }
91     sb.append(")");
92     return sb.toString();
93   }
94
95   @Override
96   public int hashCode()
97   {
98     int h = command.hashCode();
99     if (parameters != null)
100     {
101       for (String p : parameters)
102       {
103         h = h * 37 + p.hashCode();
104       }
105     }
106     return h;
107   }
108
109   /**
110    * Answers true if {@code obj} is a {@code StructureCommand} with the same
111    * command and parameters as this one, else false
112    */
113   @Override
114   public boolean equals(Object obj)
115   {
116     if (obj == null || !(obj instanceof StructureCommand))
117     {
118       return false;
119     }
120     StructureCommand sc = (StructureCommand) obj;
121
122     if (!command.equals(sc.command))
123     {
124       return false;
125     }
126     if (parameters == null || sc.parameters == null)
127     {
128       return (parameters == null) && (sc.parameters == null);
129     }
130
131     int j = parameters.size();
132     if (j != sc.parameters.size())
133     {
134       return false;
135     }
136     for (int i = 0; i < j; i++)
137     {
138       if (!parameters.get(i).equals(sc.parameters.get(i)))
139       {
140         return false;
141       }
142     }
143     return true;
144   }
145
146 }