JAL-4090 JAL-1551 spotlessApply
[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   private boolean waitNeeded = false;
33
34   public StructureCommand(String cmd, String... params)
35   {
36     command = cmd;
37     if (params != null)
38     {
39       for (String p : params)
40       {
41         addParameter(p);
42       }
43     }
44   }
45
46   public void setWaitNeeded(boolean wait)
47   {
48     waitNeeded = wait;
49   }
50
51   @Override
52   public boolean isWaitNeeded()
53   {
54     return waitNeeded;
55   }
56
57   @Override
58   public void addParameter(String param)
59   {
60     if (parameters == null)
61     {
62       parameters = new ArrayList<>();
63     }
64     parameters.add(param);
65   }
66
67   @Override
68   public String getCommand()
69   {
70     return command;
71   }
72
73   @Override
74   public List<String> getParameters()
75   {
76     return parameters;
77   }
78
79   @Override
80   public boolean hasParameters()
81   {
82     return parameters != null && !parameters.isEmpty();
83   }
84
85   @Override
86   public String toString()
87   {
88     if (!hasParameters())
89     {
90       return command;
91     }
92     StringBuilder sb = new StringBuilder(32);
93     sb.append(command).append("(");
94     boolean first = true;
95     for (String p : parameters)
96     {
97       if (!first)
98       {
99         sb.append(",");
100       }
101       first = false;
102       sb.append(p);
103     }
104     sb.append(")");
105     return sb.toString();
106   }
107
108   @Override
109   public int hashCode()
110   {
111     int h = command.hashCode();
112     if (parameters != null)
113     {
114       for (String p : parameters)
115       {
116         h = h * 37 + p.hashCode();
117       }
118     }
119     return h;
120   }
121
122   /**
123    * Answers true if {@code obj} is a {@code StructureCommand} with the same
124    * command and parameters as this one, else false
125    */
126   @Override
127   public boolean equals(Object obj)
128   {
129     if (obj == null || !(obj instanceof StructureCommand))
130     {
131       return false;
132     }
133     StructureCommand sc = (StructureCommand) obj;
134
135     if (!command.equals(sc.command))
136     {
137       return false;
138     }
139     if (parameters == null || sc.parameters == null)
140     {
141       return (parameters == null) && (sc.parameters == null);
142     }
143
144     int j = parameters.size();
145     if (j != sc.parameters.size())
146     {
147       return false;
148     }
149     for (int i = 0; i < j; i++)
150     {
151       if (!parameters.get(i).equals(sc.parameters.get(i)))
152       {
153         return false;
154       }
155     }
156     return true;
157   }
158
159 }