Java Program to Implement SimpeBindings API

This Java program is to Implement SimpleBindings API.A simple implementation of Bindings backed by a HashMap or some other specified Map.

Here is the source code of the Java program to Implement SimpleBindings API. The Java program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. import java.util.Collection;
  2. import java.util.HashMap;
  3. import java.util.Iterator;
  4. import java.util.Map;
  5. import java.util.Set;
  6. import java.util.Map.Entry;
  7. import javax.script.SimpleBindings;
  8.  
  9. public class SimpleBindingsImpl
  10. {
  11.     private SimpleBindings simpleBindings;
  12.  
  13.     /** Default constructor uses a HashMap. **/
  14.     public SimpleBindingsImpl()
  15.     {
  16.         simpleBindings = new SimpleBindings();
  17.     }
  18.  
  19.     /** Constructor uses an existing Map to store the values. **/
  20.     public SimpleBindingsImpl(Map<String, Object> m)
  21.     {
  22.         simpleBindings = new SimpleBindings(m);
  23.     }
  24.  
  25.     /** Removes all of the mappings from this map. **/
  26.     public void clear()
  27.     {
  28.         simpleBindings.clear();
  29.     }
  30.  
  31.     /** Returns true if this map contains a mapping for the specified key. **/
  32.     public boolean containsKey(Object key)
  33.     {
  34.         return simpleBindings.containsKey(key);
  35.     }
  36.  
  37.     /** Returns true if this map maps one or more keys to the specified value. **/
  38.     public boolean containsValue(Object value)
  39.     {
  40.         return simpleBindings.containsValue(value);
  41.     }
  42.  
  43.     /** Returns a Set view of the mappings contained in this map. **/
  44.     public Set<Map.Entry<String, Object>> entrySet()
  45.     {
  46.         return simpleBindings.entrySet();
  47.     }
  48.  
  49.     /** 
  50.      * Returns the value to which the specified key is mapped, or null if this 
  51.      * map contains no mapping for the key.
  52.      **/
  53.     public Object get(Object key)
  54.     {
  55.         return simpleBindings.get(key);
  56.     }
  57.  
  58.     /** Returns true if this map contains no key-value mappings. **/
  59.     public boolean isEmpty()
  60.     {
  61.         return simpleBindings.isEmpty();
  62.     }
  63.  
  64.     /** Returns a Set view of the keys contained in this map. **/
  65.     public Set<String> keySet()
  66.     {
  67.         return simpleBindings.keySet();
  68.     }
  69.  
  70.     /** Associates the specified value with the specified key in this map. **/
  71.     public Object put(String key, Object value)
  72.     {
  73.         return simpleBindings.put(key, value);
  74.     }
  75.  
  76.     /** Copies all of the mappings from the specified map to this map. **/
  77.     public void putAll(Map<? extends String, ? extends Object> m)
  78.     {
  79.         simpleBindings.putAll(m);
  80.     }
  81.  
  82.     /** Removes the mapping for the specified key from this map if present. **/
  83.     public Object remove(Object key)
  84.     {
  85.         return simpleBindings.remove(key);
  86.     }
  87.  
  88.     /** Returns the number of key-value mappings in this map. **/
  89.     public int size()
  90.     {
  91.         return simpleBindings.size();
  92.     }
  93.  
  94.     /** Returns a Collection view of the values contained in this map. **/
  95.     public Collection<Object> values()
  96.     {
  97.         return simpleBindings.values();
  98.     }
  99.  
  100.     public static void main(String... arg)
  101.     {
  102.         SimpleBindingsImpl simpleBindings = new SimpleBindingsImpl();
  103.         simpleBindings.put("one", 100);
  104.         simpleBindings.put("two", 200);
  105.         simpleBindings.put("three", 300);
  106.  
  107.         Map<String, Object> anotherMap = new HashMap<String, Object>(); 
  108.         anotherMap.put("four", 400);
  109.         anotherMap.put("five", 500);
  110.         simpleBindings.putAll(anotherMap);
  111.  
  112.         System.out.println("the key set of the simpleBindings is ");
  113.         Set<String> keySet = simpleBindings.keySet();
  114.         Iterator<String> itr = keySet.iterator();
  115.         while (itr.hasNext())
  116.         {
  117.             System.out.print(itr.next() + "\t");
  118.         }
  119.         System.out.println();
  120.  
  121.         System.out.println("the values of the simpleBindings is ");
  122.         Collection<Object> collectionValues = simpleBindings.values(); 
  123.         Iterator<Object> citr = collectionValues.iterator();
  124.         while (citr.hasNext())
  125.         {
  126.             System.out.print(citr.next() + "\t");
  127.         }
  128.         System.out.println();
  129.  
  130.         System.out.println("the entry set of the simpleBindings is ");
  131.         Iterator<Entry<String, Object>> eitr;
  132.         Set<Entry<String, Object>> entrySet = simpleBindings.entrySet();
  133.         eitr = entrySet.iterator();
  134.         while (eitr.hasNext())
  135.         {
  136.             System.out.println(eitr.next() + "\t");
  137.         }
  138.         System.out.println("the simpleBindings contains Key \"three\" :"
  139.            +  simpleBindings.containsKey("three"));
  140.         System.out.println("the simpleBindings contains Value 600 :"
  141.            + simpleBindings.containsValue(600));
  142.         System.out.println("the size of the simpleBindings is "
  143.            + simpleBindings.size());
  144.         simpleBindings.clear();
  145.         if (simpleBindings.isEmpty())
  146.             System.out.println("the simpleBindings is empty");
  147.         else
  148.             System.out.println("the simpleBindings is not empty");
  149.     }
  150. }

$ javac SimpleBindingsImpl.java
$ java SimpleBindingsImpl
the key set of the simpleBindings is 
two	five	one	three	four	
the values of the simpleBindings is 
200	500	100	300	400	
the entry set of the simpleBindings is 
two=200	
five=500	
one=100	
three=300	
four=400	
the simpleBindings contains Key "three" :true
the simpleBindings contains Value 600 :false
the size of the simpleBindings is 5
the simpleBindings is empty

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.