//
// CIMFlavor.java
//
// The contents of this file are subject to the SNIA Public License Version 1.0
// (the "License"); you may not use this file except in compliance with the
// License. You may obtain a copy of the License at
//
//	http://www.snia.org/resources/openSource.html
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the 
// License for the specific language governing rights and limitations
// under the License.
//
// The Original Code is CIMFlavor.java
// 
// The Initial Developer of the Original Code is David Simons.
//
// Contributor(s): Peter Rivera
//
//
// This source uses the Solaris WBEM APIs version 2.2. 
// Synchronization will be maintained with the Solaris WBEM APIS,
// which are being standardized through the Java Community Process.
//
//
// $Id: CIMFlavor.java,v 1.1 2003/07/17 18:32:55 kumpf Exp $
//
// $Log: CIMFlavor.java,v $
// Revision 1.1  2003/07/17 18:32:55  kumpf
// HP-BAPU: PEP-64 CIM Client and CIM Listener Java Interface
//
// Revision 1.1.1.1  2001/10/31 18:14:21  karl
// import of SNIA 0_7
//
// Revision 1.8  2001/01/12 17:41:36  davidsi
// - Backed off the DISABLEOVERRIDE portion of 1.7 - the check is necessary
//   for the write xml encodings to be built.
//
// Revision 1.7  2001/01/02 19:30:21  davidsi
// - Fixed LASTVALUE which was off by one.
// - Removed check for DISABLEOVERRIDE which does not exist.
//
// Revision 1.6  2000/08/16 05:59:52  shawn
// merge P_TOMCAT31 branch
//
// Revision 1.5.6.1  2000/08/10 00:03:27  shawn
// head merge
//
// Revision 1.5  2000/07/10 16:55:16  davidsi
// - Added exception to constructor when flavor is not valid.
//
// Revision 1.4  2000/05/11 10:12:19  davidsi
// - Fixed tabs.
//
// Revision 1.3  2000/05/06 17:04:21  davidsi
// - Modifications to bring classes inline with the SUN WBEM APIs 2.2.
//
// Revision 1.2  2000/02/13 15:09:07  davidsi
// - Added text to cover re-use of Solaris WBEM APIs.
//
// Revision 1.1.1.1  2000/02/06 16:41:03  davidsi
// Initial version.
//
//
//
package org.snia.wbem.cim;

import java.io.Serializable;
import java.net.MalformedURLException;

/**
    Creates and instantiates a CIM flavor, a characteristic of a qualifier
    that describes overriding and inheritance rules.  These rules specify
    how qualifiers are transmitted from classes to derived classes, or
    from classes to instances.
 */

public class CIMFlavor extends Object implements Serializable {
    public static final int ENABLEOVERRIDE=1;	// overridable
    public static final int DISABLEOVERRIDE=2;	// cannot be overriden
    public static final int RESTRICTED=3;	// applies only to the declaring class
    public static final int TOSUBCLASS=4;	// qualifier is inherited
    public static final int TRANSLATE=5;	// qualifier can be specified in multiple locales
    private static final int LASTVALUE=6;
    private static CIMFlavor[] predefined= new CIMFlavor[10];
    private int flavor=0;
    
    static {
	int cnt=0;
	
	predefined[cnt++]=new CIMFlavor(ENABLEOVERRIDE);
	predefined[cnt++]=new CIMFlavor(DISABLEOVERRIDE);
	predefined[cnt++]=new CIMFlavor(RESTRICTED);
	predefined[cnt++]=new CIMFlavor(TOSUBCLASS);
	predefined[cnt++]=new CIMFlavor(TRANSLATE);
    }
    
    private boolean validScope(int iflavor) {
	//
	// NOTE: has implicit knowledge of flavor values!
	//
	return(iflavor>=ENABLEOVERRIDE && iflavor<LASTVALUE);
    }
    
    // CIMFlavor 
    // 
    public CIMFlavor(int iflavor) {
	//
	// Value only gets set if its valid
	//
	if ( !validScope(iflavor) )
	    throw new InstantiationError("Not a valid flavor");

	flavor=iflavor;
    }
     
    // getFlavor 
    //
    // Returns a CIMFlavor object
    //
    public static CIMFlavor getFlavor(int iflavor) {
	int i;
	
	for(i=0;i<predefined.length;i++) {
	    if (predefined[i].flavor==iflavor)
		return(predefined[i]);
	}
	return(null);
    }
 
    // getFlavor 
    //
    // Returns the qualifier flavor as an integer
    //
    public int getFlavor() {
	return(flavor);
    }

    // 
    // equals
    //
    // Returns true if object is a valid CIM flavor
    public boolean equals(Object o) {
	return (o instanceof CIMFlavor &&
	    ((CIMFlavor)o).flavor==flavor );
    }
 
    // toString 
    //
    public String toString() {
	return("FLAVOR("+toMOF()+")");
    }

    // toMOF 
    public String toMOF() {
	if (flavor==ENABLEOVERRIDE)
	    return("ENABLEOVERRIDE");
	if (flavor==DISABLEOVERRIDE)
	    return("DISABLEOVERRIDE");
	if (flavor==RESTRICTED)
	    return("RESTRICTED");
	if (flavor==TOSUBCLASS)
	    return("TOSUBCLASS");
	if (flavor==RESTRICTED)
	    return("RESTRICTED");
	if (flavor==TRANSLATE)
	    return("TRANSLATE");
	return("BAD FLAVOR");
    }

    // toXml 
    public String toXml() throws MalformedURLException {
	if (flavor==ENABLEOVERRIDE)
	    return("OVERRIDABLE");
	/*
	 * The next return value looks strange but the intent is that
	 * the caller assigns false to the value rather than true, hence
	 * disabling the override 
	 */ 
	if (flavor==DISABLEOVERRIDE)
	    return("OVERRIDABLE");
	if (flavor==TOSUBCLASS)
	    return("TOSUBCLASS");
	if (flavor==RESTRICTED)
	    return("RESTRICTED");
	if (flavor==TRANSLATE)
	    return("TRANSLATABLE");
	throw new MalformedURLException("No such flavor");
    }
} 
