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