JAL-3048 jalview.utils.dialogrunner.DialogRunner allows sequences of runnable methods...
[jalview.git] / src / jalview / util / dialogrunner / Response.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.util.dialogrunner;
22
23 public class Response
24 {
25   int type = 0; // int = 0, String = 1, Object = 2;
26
27   int intresp;
28
29   String stringresp;
30
31   Object objresp;
32
33   public Response(int response)
34   {
35     type = 0;
36     intresp = response;
37   }
38
39   public Response(String response)
40   {
41     type = 1;
42     stringresp = response;
43   }
44
45   public Response(Object response)
46   {
47     if (response instanceof String)
48     {
49       type = 1;
50       stringresp = (String) response;
51       return;
52     }
53     if (response instanceof Integer)
54     {
55       type = 0;
56       intresp = ((Integer) response).intValue();
57       return;
58     }
59     objresp = response;
60     type = 2;
61   }
62
63   @Override
64   public boolean equals(Object obj)
65   {
66     if (obj == null || !(obj instanceof Response))
67     {
68       return false;
69     }
70     ;
71     if (((Response) obj).type == type)
72     {
73       switch (type)
74       {
75       case 0:
76         return ((((Response) obj).intresp) == intresp);
77       case 1:
78         return (((Response) obj).stringresp.equals(stringresp));
79       case 2:
80         return (((Response) obj).objresp).equals(objresp);
81       }
82     }
83     return false;
84   }
85
86   @Override
87   public int hashCode()
88   {
89     switch (type)
90     {
91     case 0:
92       return Integer.valueOf(intresp).hashCode();
93     case 1:
94       return stringresp.hashCode();
95     case 2:
96       return objresp.hashCode();
97     }
98     return super.hashCode();
99   }
100
101   @Override
102   public String toString()
103   {
104     switch (type)
105     {
106     case 0:
107       return "DialogRunner int: " + intresp;
108     case 1:
109       return "DialogRunner str: '" + stringresp + "'";
110     case 2:
111       return "DialogRunner obj: " + objresp.toString();
112     }
113     return "Unconfigured response.";
114   }
115 }