JAL-629 Added --opennew --nonews --nosplash. Added java globbing for = e.g. --open...
[jalview.git] / src / jalview / bin / argparser / SubVals.java
1 package jalview.bin.argparser;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.List;
6 import java.util.Map;
7
8 import jalview.bin.Console;
9
10 /**
11  * A helper class to parse a string of the possible forms "content"
12  * "[index]content", "[keyName=keyValue]content" and return the integer index,
13  * the strings keyName and keyValue, and the content after the square brackets
14  * (if present). Values not set `will be -1 or null.
15  */
16 public class SubVals
17 {
18   private static int NOTSET = -1;
19
20   private int index = NOTSET;
21
22   private Map<String, String> subVals = null;
23
24   private static char SEPARATOR = ';';
25
26   private static char EQUALS = '=';
27
28   private String content = null;
29
30   public SubVals(Map<String, String> sv, String c)
31   {
32     this.subVals = sv;
33     this.content = c;
34   }
35
36   public SubVals(String item)
37   {
38     this.parseVals(item);
39   }
40
41   public void parseVals(String item)
42   {
43     if (item == null)
44       return;
45     if (item.indexOf('[') == 0 && item.indexOf(']') > 1)
46     {
47       int openBracket = 0;
48       int closeBracket = item.indexOf(']');
49       String subvalsString = item.substring(openBracket + 1, closeBracket);
50       this.content = item.substring(closeBracket + 1);
51       boolean setIndex = false;
52       for (String subvalString : subvalsString
53               .split(Character.toString(SEPARATOR)))
54       {
55         if (subVals == null)
56           subVals = new HashMap<>();
57         int equals = subvalString.indexOf(EQUALS);
58         if (equals > -1)
59         {
60           this.put(subvalString.substring(0, equals),
61                   subvalString.substring(equals + 1));
62         }
63         else
64         {
65           try
66           {
67             this.index = Integer.parseInt(subvalString);
68             setIndex = true;
69           } catch (NumberFormatException e)
70           {
71             // store this non-numeric key as a "true" value
72             subVals.put(subvalString, "true");
73           }
74         }
75       }
76       if (!setIndex)
77         this.index = NOTSET;
78       else
79         Console.debug("SubVals from '" + subvalsString + "' has index "
80                 + this.index + " set");
81     }
82     else
83     {
84       this.content = item;
85     }
86   }
87
88   protected void put(String key, String val)
89   {
90     if (subVals == null)
91       subVals = new HashMap<>();
92     subVals.put(key, val);
93   }
94
95   public boolean notSet()
96   {
97     // notSet is true if content present but nonsensical
98     return index == NOTSET && subVals == null;
99   }
100
101   public String get(String key)
102   {
103     return subVals == null ? null : subVals.get(key);
104   }
105
106   public boolean has(String key)
107   {
108     return subVals == null ? false : subVals.containsKey(key);
109   }
110
111   public int getIndex()
112   {
113     return index;
114   }
115
116   public String getContent()
117   {
118     return content;
119   }
120
121   protected Map<String, String> getSubValsMap()
122   {
123     return subVals;
124   }
125
126   public String toString()
127   {
128     if (subVals == null && getIndex() == NOTSET)
129       return "";
130
131     StringBuilder sb = new StringBuilder();
132     List<String> entries = new ArrayList<>();
133     subVals.entrySet().stream().forEachOrdered(
134             m -> entries.add(m.getValue().equals("true") ? m.getKey()
135                     : new StringBuilder().append(m.getKey()).append(EQUALS)
136                             .append(m.getValue()).toString()));
137     if (getIndex() != NOTSET)
138       entries.add(Integer.toString(getIndex()));
139     sb.append('[');
140     sb.append(String.join(Character.toString(SEPARATOR), entries));
141     sb.append(']');
142     return sb.toString();
143   }
144 }