View Javadoc
1   // ******************************************************************************
2   //
3   // Title:       Force Field X.
4   // Description: Force Field X - Software for Molecular Biophysics.
5   // Copyright:   Copyright (c) Michael J. Schnieders 2001-2025.
6   //
7   // This file is part of Force Field X.
8   //
9   // Force Field X is free software; you can redistribute it and/or modify it
10  // under the terms of the GNU General Public License version 3 as published by
11  // the Free Software Foundation.
12  //
13  // Force Field X is distributed in the hope that it will be useful, but WITHOUT
14  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16  // details.
17  //
18  // You should have received a copy of the GNU General Public License along with
19  // Force Field X; if not, write to the Free Software Foundation, Inc., 59 Temple
20  // Place, Suite 330, Boston, MA 02111-1307 USA
21  //
22  // Linking this library statically or dynamically with other modules is making a
23  // combined work based on this library. Thus, the terms and conditions of the
24  // GNU General Public License cover the whole combination.
25  //
26  // As a special exception, the copyright holders of this library give you
27  // permission to link this library with independent modules to produce an
28  // executable, regardless of the license terms of these independent modules, and
29  // to copy and distribute the resulting executable under terms of your choice,
30  // provided that you also meet, for each linked independent module, the terms
31  // and conditions of the license of that module. An independent module is a
32  // module which is not derived from or based on this library. If you modify this
33  // library, you may extend this exception to your version of the library, but
34  // you are not obligated to do so. If you do not wish to do so, delete this
35  // exception statement from your version.
36  //
37  // ******************************************************************************
38  package ffx.utilities;
39  
40  import java.util.LinkedHashMap;
41  import java.util.Map;
42  
43  /**
44   * The FFXBinding binds variable names to their instances for an FFX Command.
45   *
46   * @author Michael J. Schnieders
47   */
48  public class FFXBinding {
49  
50    private Map<String, Object> variables;
51  
52    /**
53     * <p>Constructor for FFXContext.</p>
54     */
55    public FFXBinding() {
56      super();
57    }
58  
59    /**
60     * <p>Constructor for FFXContext.</p>
61     *
62     * @param variables a {@link java.util.Map} object
63     */
64    public FFXBinding(Map<String, Object> variables) {
65      this();
66      this.variables = variables;
67    }
68  
69    /**
70     * A helper constructor used in main(String[]) method calls
71     *
72     * @param args are the command line arguments from a main()
73     */
74    public FFXBinding(String[] args) {
75      this();
76      variables = new LinkedHashMap<>();
77      variables.put("args", args);
78    }
79  
80    /**
81     * <p>getVariable.</p>
82     *
83     * @param name the name of the variable to lookup
84     * @return the variable value
85     */
86    public Object getVariable(String name) {
87      if (variables == null) {
88        return null;
89      }
90  
91      return variables.get(name);
92    }
93  
94    /**
95     * Sets the value of the given variable
96     *
97     * @param name  the name of the variable to set
98     * @param value the new value for the given variable
99     */
100   public void setVariable(String name, Object value) {
101     if (variables == null) {
102       variables = new LinkedHashMap<>();
103     }
104     variables.put(name, value);
105   }
106 
107   /**
108    * Remove the variable with the specified name.
109    *
110    * @param name the name of the variable to remove
111    */
112   public void removeVariable(String name) {
113     if (null == variables) {
114       return;
115     }
116 
117     variables.remove(name);
118   }
119 
120   /**
121    * Simple check for whether the context contains a particular variable or not.
122    *
123    * @param name the name of the variable to check for
124    * @return a boolean
125    */
126   public boolean hasVariable(String name) {
127     return variables != null && variables.containsKey(name);
128   }
129 
130   /**
131    * Get the Map of all variables. This returns a reference and not a copy.
132    *
133    * @return a Map of the variables.
134    */
135   public Map<String, Object> getVariables() {
136     if (variables == null) {
137       variables = new LinkedHashMap<>();
138     }
139     return variables;
140   }
141 
142   /**
143    * Set the Map of all variables. The reference is kept without making a copy.
144    *
145    * @param variables a Map of the variables.
146    */
147   public void setVariables(Map<String, Object> variables) {
148     this.variables = variables;
149   }
150 
151 }
152