Open In App

Java Program to Implement JobStateReasons API

Improve
Improve
Like Article
Like
Save
Share
Report

An API stands for Application Programming Interface which are simply a set of definitions and protocols for building and integrating application software to simplify app development to save time and money. For instance, if we want to forecast the weather of different cities using past data, instead of finding and typing all the past data manually, we can use weather APIs’ to fasten and improvise the process.

JobState is a printing attribute class, rather an enumeration that identifies the current state of a print job. The class JobState defines standard job state values. A Print Service implementation only needs to report those job states which are appropriate for the particular implementation. It does not have to report every defined job state. The JobStateReasons attribute tends to augment the JobState attribute to ensure more detailed information about the job in the given job state.

Implementation: JobStateReasons API through java program 

Java




// Java Program to Implementing JobStateReasons API
  
// Importing java libraries
import java.util.Collection;
import javax.print.attribute.Attribute;
import javax.print.attribute.standard.JobStateReason;
import javax.print.attribute.standard.JobStateReasons;
  
public class GFG {
    private JobStateReasons jobStateReasons;
  
    // Construct a new, empty job state reasons attribute;
    // the underlying hash set has the default initial
    // capacity and load factor
    public GFG()
    {
        jobStateReasons = new JobStateReasons();
    }
  
    // Construct a new job state reasons
    // attribute that contains the same
    // JobStateReason objects as the given collection
    public GFG(Collection<JobStateReason> collection)
    {
        jobStateReasons = new JobStateReasons(collection);
    }
  
    // Construct a new, empty job state reasons attribute;
    // the underlying hash set has the given
    // initial capacity and the default load
    // factor.
    public GFG(int initialCapacity)
    {
        jobStateReasons
            = new JobStateReasons(initialCapacity);
    }
  
    // Construct a new, empty job state reasons attribute;
    // the underlying hash
    // set has the given initial capacity and load factor
    public GFG(int initialCapacity, float loadFactor)
    {
        jobStateReasons = new JobStateReasons(
            initialCapacity, loadFactor);
    }
  
    // Adds the specified element to this job state reasons
    // attribute if it is not already present.
    public boolean add(JobStateReason o)
    {
        return jobStateReasons.add(o);
    }
  
    // Get the printing attribute class which is to be used
    // as the "category" for this printing attribute value.
  
    public Class<? extends Attribute> getCategory()
    {
        return jobStateReasons.getCategory();
    }
  
    // Get the name of the category of which this attribute
    // value is an instance
    public String getName()
    {
        return jobStateReasons.getName();
    }
  
    // Return true if this set contains the specified
    // element
    public boolean contains(Object obj)
    {
        return jobStateReasons.contains(obj);
    }
  
    // Returns true if the set is empty
    public boolean isEmpty()
    {
        return jobStateReasons.isEmpty();
    }
  
    // Removes the specified element from
    // this set if present
    public boolean remove(Object obj)
    {
        return jobStateReasons.remove(obj);
    }
  
    // Returns the number of elements in set
    public int size() { return jobStateReasons.size(); }
  
    // Removes all elements from this set
    public void clear() { jobStateReasons.clear(); }
  
    // Returns an array containing all of the elements in
    // this set
    public Object[] toArray()
    {
        return jobStateReasons.toArray();
    }
  
    // Main driver function
    public static void main(String args[])
    {
        // Creating object of class
        GFG jobStateReasons = new GFG();
  
        jobStateReasons.add(
            JobStateReason.COMPRESSION_ERROR);
        jobStateReasons.add(
            JobStateReason.JOB_CANCELED_BY_USER);
        jobStateReasons.add(
            JobStateReason.JOB_COMPLETED_WITH_WARNINGS);
        jobStateReasons.add(
            JobStateReason.DOCUMENT_FORMAT_ERROR);
  
        // Display messages
        System.out.println("Category Name: "
                           + jobStateReasons.getName());
        System.out.println();
        System.out.println("The JobStateReasons are: ");
  
        Object[] jobs = (Object[])jobStateReasons.toArray();
  
        for (int i = 0; i < jobs.length; i++) {
            System.out.println((i + 1) + ") " + jobs[i]);
        }
        System.out.println();
  
        jobStateReasons.clear();
        System.out.println(
            "all JobStateReasons are cleared");
  
        if (jobStateReasons.isEmpty())
            System.out.println(
                "jobStateReasons is now empty");
        else
            System.out.println(
                "jobStateReasons is not empty yet");
    }
}


Output:

Category Name: job-state-reasons

The JobStateReasons are: 
1) compression-error
2) document-format-error
3) job-completed-with-warnings
4) job-canceled-by-user

all JobStateReasons are cleared
jobStateReasons is now empty


Last Updated : 08 Dec, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads