Java Program to Implement PrinterStateReasons API

This Java program is to Implement PrinterStateReasons API.Class PrinterStateReasons is a printing attribute class, a set of enumeration values, that provides additional information about the printer’s current state, i.e., information that augments the value of the printer’s PrinterState attribute.

Here is the source code of the Java program to Implement PrinterStateReasons 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.Iterator;
  3. import java.util.Map;
  4. import java.util.Map.Entry;
  5. import java.util.Set;
  6. import javax.print.attribute.Attribute;
  7. import javax.print.attribute.standard.PrinterStateReason;
  8. import javax.print.attribute.standard.PrinterStateReasons;
  9. import javax.print.attribute.standard.Severity;
  10.  
  11. public class PrinterStateReasonsImpl
  12. {
  13.     private PrinterStateReasons printerStateReasons;
  14.  
  15.     /**
  16.      * Construct a new, empty printer state reasons attribute; the underlying
  17.      * hash map has the default initial capacity and load factor.
  18.      **/
  19.     public PrinterStateReasonsImpl()
  20.     {
  21.         printerStateReasons = new PrinterStateReasons();
  22.     }
  23.  
  24.     /**
  25.      * super a new, empty printer state reasons attribute; the underlying hash
  26.      * map has the given initial capacity and the default load factor.
  27.      **/
  28.     public PrinterStateReasonsImpl(int initialCapacity)
  29.     {
  30.         printerStateReasons = new PrinterStateReasons(initialCapacity);
  31.     }
  32.  
  33.     /**
  34.      * Construct a new, empty printer state reasons attribute; the underlying
  35.      * hash map has the given initial capacity and load factor.
  36.      **/
  37.     public PrinterStateReasonsImpl(int initialCapacity, float loadFactor)
  38.     {
  39.         printerStateReasons = new PrinterStateReasons(initialCapacity, loadFactor);
  40.     }
  41.  
  42.     /**
  43.      * Construct a new printer state reasons attribute that contains the same
  44.      * PrinterStateReason-to-Severity mappings as the given map.
  45.      **/
  46.     public PrinterStateReasonsImpl(Map<PrinterStateReason, Severity> map)
  47.     {
  48.         printerStateReasons = new PrinterStateReasons(map);
  49.     }
  50.  
  51.     /** Removes all of the mappings from this map. **/
  52.     public void clear()
  53.     {
  54.         printerStateReasons.clear();
  55.     }
  56.  
  57.     /**
  58.      * Returns a shallow copy of this HashMap instance: the keys and values
  59.      * themselves are not cloned.
  60.      **/
  61.     public Object clone()
  62.     {
  63.         return printerStateReasons.clone();
  64.     }
  65.  
  66.     /** Returns true if this map contains a mapping for the specified key. **/
  67.     public boolean containsKey(Object key)
  68.     {
  69.         return printerStateReasons.containsKey(key);
  70.     }
  71.  
  72.     /** Returns true if this map maps one or more keys to the specified value. **/
  73.     public boolean containsValue(Object value)
  74.     {
  75.         return printerStateReasons.containsValue(value);
  76.     }
  77.  
  78.     /** Returns a Set view of the mappings contained in this map. **/
  79.     public Set<Entry<PrinterStateReason, Severity>> entrySet()
  80.     {
  81.         return printerStateReasons.entrySet();
  82.     }
  83.  
  84.     /**
  85.      * Returns the value to which the specified key is mapped, or null if this
  86.      * map contains no mapping for the key.
  87.      **/
  88.     public Severity get(Object key)
  89.     {
  90.         return printerStateReasons.get(key);
  91.     }
  92.  
  93.     /** Returns true if this map contains no key-value mappings. **/
  94.     public boolean isEmpty()
  95.     {
  96.         return printerStateReasons.isEmpty();
  97.     }
  98.  
  99.     /** Returns a Set view of the keys contained in this map. **/
  100.     public Set<PrinterStateReason> keySet()
  101.     {
  102.         return printerStateReasons.keySet();
  103.     }
  104.  
  105.     /** Associates the specified value with the specified key in this map. **/
  106.     public Severity put(PrinterStateReason reason, Severity severity)
  107.     {
  108.         return printerStateReasons.put(reason, severity);
  109.     }
  110.  
  111.     /** Copies all of the mappings from the specified map to this map. **/
  112.     public void putAll(Map<? extends PrinterStateReason, ? extends Severity> m)
  113.     {
  114.         printerStateReasons.putAll(m);
  115.     }
  116.  
  117.     /** Removes the mapping for the specified key from this map if present. **/
  118.     public Severity remove(Object key)
  119.     {
  120.         return printerStateReasons.remove(key);
  121.     }
  122.  
  123.     /** Returns the number of key-value mappings in this map. **/
  124.     public int size()
  125.     {
  126.         return printerStateReasons.size();
  127.     }
  128.  
  129.     /** Returns a Collection view of the values contained in this map. **/
  130.     public Collection<Severity> values()
  131.     {
  132.         return printerStateReasons.values();
  133.     }
  134.  
  135.     /**
  136.      * Get the printing attribute class which is to be used as the "category"
  137.      * for this printing attribute value.
  138.      **/
  139.     public Class<? extends Attribute> getCategory()
  140.     {
  141.         return printerStateReasons.getCategory();
  142.     }
  143.  
  144.     /**
  145.      * Get the name of the category of which this attribute value is an
  146.      * instance.
  147.      **/
  148.     public String getName()
  149.     {
  150.         return printerStateReasons.getName();
  151.     }
  152.  
  153.     /**
  154.      * Obtain an unmodifiable set view of the individual printer state reason
  155.      * attributes at the given severity level in this PrinterStateReasons
  156.      * attribute.
  157.      **/
  158.     public Set<PrinterStateReason> printerStateReasonSet(Severity severity)
  159.     {
  160.         return printerStateReasons.printerStateReasonSet(severity);
  161.     }
  162.  
  163.     public static void main(String... arg)
  164.     {
  165.         PrinterStateReasonsImpl printerStateReasons = new PrinterStateReasonsImpl();
  166.         printerStateReasons.put(PrinterStateReason.CONNECTING_TO_DEVICE,Severity.ERROR);
  167.         printerStateReasons.put(PrinterStateReason.COVER_OPEN,Severity.REPORT);
  168.         printerStateReasons.put(PrinterStateReason.INPUT_TRAY_MISSING, Severity.WARNING);
  169.  
  170.         System.out.println("the key set of the printerStateReasons is ");
  171.         Set<PrinterStateReason> keySet = printerStateReasons.keySet();
  172.         Iterator<PrinterStateReason> itr = keySet.iterator();
  173.         while (itr.hasNext())
  174.         {
  175.             System.out.print(itr.next() + "\t");
  176.         }
  177.         System.out.println();
  178.  
  179.         System.out.println("the values of the printerStateReasons is ");
  180.         Collection<Severity> collectionValues = printerStateReasons.values();
  181.         Iterator<Severity > citr = collectionValues.iterator();
  182.         while (citr.hasNext())
  183.         {
  184.             System.out.print(citr.next() + "\t");
  185.         }
  186.         System.out.println();
  187.         System.out.println("the entry set of the printerStateReasons is ");
  188.         Iterator<Entry<PrinterStateReason, Severity>> eitr;
  189.         Set<Entry<PrinterStateReason, Severity>> entrySet = printerStateReasons.entrySet();
  190.         eitr = entrySet.iterator();
  191.         while (eitr.hasNext())
  192.         {
  193.             System.out.println(eitr.next() + "\t");
  194.         }
  195.         System.out.println("the printerStateReasons contains Key CONNECTING_TO_DEVICE :" 
  196.             + printerStateReasons.containsKey(PrinterStateReason.CONNECTING_TO_DEVICE));
  197.         System.out.println("the printerStateReasons contains Value ERROR :"
  198.             + printerStateReasons.containsValue(Severity.ERROR));
  199.         System.out.println("the size of the printerStateReasons is " + printerStateReasons.size());
  200.         printerStateReasons.clear();
  201.         if (printerStateReasons.isEmpty())
  202.             System.out.println("the printerStateReasons is empty");
  203.         else
  204.             System.out.println("the printerStateReasons is not empty");
  205.     }
  206. }

$ javac PrinterStateReasonsImpl.java
$ java PrinterStateReasonsImpl
the key set of the printerStateReasons is 
input-tray-missing	connecting-to-device	cover-open	
the values of the printerStateReasons is 
warning	error	report	
the entry set of the printerStateReasons is 
input-tray-missing=warning	
connecting-to-device=error	
cover-open=report	
the printerStateReasons contains Key CONNECTING_TO_DEVICE :true
the printerStateReasons contains Value ERROR :true
the size of the printerStateReasons is 3
the printerStateReasons 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.