org/objectweb/proactive/core/xml/VariableContractType.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
00028  * 
00029  * ################################################################
00030  */ 
00031 package org.objectweb.proactive.core.xml;
00032 
00033 import org.objectweb.proactive.core.descriptor.xml.ProActiveDescriptorConstants;
00034 
00035 
00042 public class VariableContractType {
00043     static final public VariableContractType DescriptorVariable = new VariableContractType(0,
00044             new String[] { "Descriptor", "Program" },
00045             new String[] { "Descriptor" }, new String[] { "Program" });
00046     static final public VariableContractType ProgramVariable = new VariableContractType(1,
00047             new String[] { "Program", "Descriptor" },
00048             new String[] { "Program" }, new String[] { "Descriptor" });
00049     static final public VariableContractType JavaPropertyVariable = new VariableContractType(2,
00050             new String[] {"JavaProperty"},  new String[] {"JavaProperty"}, new String[] { "Program", "Descriptor" } );
00051     static final public VariableContractType ProgramDefaultVariable = new VariableContractType(3,
00052             new String[] { "Descriptor", "Program" },
00053             new String[] { "Descriptor", "Program" }, new String[0]);
00054     static final public VariableContractType DescriptorDefaultVariable = new VariableContractType(4,
00055             new String[] { "Program", "Descriptor" },
00056             new String[] { "Descriptor", "Program" }, new String[0]);
00057     static final public VariableContractType JavaPropertyDescriptorDefault = new VariableContractType(5,
00058             new String[] {"JavaProperty","Descriptor","Program"}, new String[] {"JavaProperty", "Descriptor", "Program"},
00059             new String[] {"Program"});
00060     static final public VariableContractType JavaPropertyProgramDefault = new VariableContractType(6,
00061                 new String[] {"JavaProperty","Program","Descriptor"}, new String[] {"JavaProperty","Program","Descriptor"},
00062                 new String[] {"Descriptor"});
00063     private int type; //Type internal identefier
00064     private String[] priority; //The lower indexed element has higher priority
00065     private String[] setAbility; //Who can set a non empty value
00066     private String[] setEmptyAbility; //Who can set an empty value
00067 
00075     private VariableContractType(int type, String[] priority,
00076         String[] setAbility, String[] setEmptyAbility) {
00077         this.type = type;
00078         this.priority = priority;
00079         this.setAbility = setAbility;
00080         this.setEmptyAbility = setEmptyAbility;
00081     }
00082 
00083     public boolean equals(Object o) {
00084         return equals((VariableContractType) o);
00085     }
00086 
00087     public boolean equals(VariableContractType b) {
00088         return this.type == b.type;
00089     }
00090 
00091     public String toString() {
00092         if (type == 0) {
00093             return ProActiveDescriptorConstants.VARIABLES_DESCRIPTOR_TAG;
00094         }
00095         if (type == 1) {
00096             return ProActiveDescriptorConstants.VARIABLES_PROGRAM_TAG;
00097         }
00098         if (type == 2) {
00099             return ProActiveDescriptorConstants.VARIABLES_JAVAPROPERTY_TAG;
00100         }
00101         if (type == 3) {
00102             return ProActiveDescriptorConstants.VARIABLES_PROGRAM_DEFAULT_TAG;
00103         }
00104         if (type == 4) {
00105             return ProActiveDescriptorConstants.VARIABLES_DESCRIPTOR_DEFAULT_TAG;
00106         }
00107         if (type == 5) {
00108             return ProActiveDescriptorConstants.VARIABLES_JAVAPROPERTY_DESCRIPTOR_TAG;
00109         }
00110         if (type == 6) {
00111             return ProActiveDescriptorConstants.VARIABLES_JAVAPROPERTY_PROGRAM_TAG;
00112         }
00113 
00114         return "UnkownVariable";
00115     }
00116 
00117     public static VariableContractType getType(String type) {
00118         if (type.equals(ProActiveDescriptorConstants.VARIABLES_DESCRIPTOR_TAG)) {
00119             return DescriptorVariable;
00120         }
00121         if (type.equals(ProActiveDescriptorConstants.VARIABLES_PROGRAM_TAG)) {
00122             return ProgramVariable;
00123         }
00124         if (type.equals(ProActiveDescriptorConstants.VARIABLES_JAVAPROPERTY_TAG)) {
00125             return JavaPropertyVariable;
00126         }
00127         if (type.equals(
00128                     ProActiveDescriptorConstants.VARIABLES_PROGRAM_DEFAULT_TAG)) {
00129             return ProgramDefaultVariable;
00130         }
00131         if (type.equals(
00132                     ProActiveDescriptorConstants.VARIABLES_DESCRIPTOR_DEFAULT_TAG)) {
00133             return DescriptorDefaultVariable;
00134         }
00135         if (type.equals(ProActiveDescriptorConstants.VARIABLES_JAVAPROPERTY_DESCRIPTOR_TAG)) {
00136             return JavaPropertyDescriptorDefault;
00137         }
00138         if (type.equals(ProActiveDescriptorConstants.VARIABLES_JAVAPROPERTY_PROGRAM_TAG)) {
00139             return JavaPropertyProgramDefault;
00140         }
00141 
00142         return null;
00143     }
00144 
00150     public boolean hasSetAbility(String from) {
00151         for (int i = 0; i < setAbility.length; i++)
00152             if (from.equalsIgnoreCase(setAbility[i])) {
00153                 return true;
00154             }
00155 
00156         return false;
00157     }
00158 
00164     public boolean hasSetEmptyAbility(String from) {
00165         for (int i = 0; i < setEmptyAbility.length; i++)
00166             if (from.equalsIgnoreCase(setEmptyAbility[i])) {
00167                 return true;
00168             }
00169 
00170         return false;
00171     }
00172 
00179     public boolean hasPriority(String current, String candidate) {
00180         int i;
00181         int j;
00182 
00183         //Find the priority of the current set value
00184         for (i = 0; i < priority.length; i++)
00185             if (current.equalsIgnoreCase(priority[i])) {
00186                 break;
00187             }
00188 
00189         //Find the priority of the replacing candidate
00190         for (j = 0; j < priority.length; j++)
00191             if (candidate.equalsIgnoreCase(priority[j])) {
00192                 break;
00193             }
00194 
00195         return j <= i;
00196     }
00197     
00198     public String getEmptyErrorMessage(String name){
00199         
00200         String canSetValue="nobody";
00201         for(int i=0;i<setAbility.length;i++){
00202                 canSetValue=setAbility[i]+" ";
00203         }
00204         
00205         return "Empty value for variable"+ this.toString() +" "+name+
00206                         " value can be set by: "+canSetValue;
00207     }
00208 }

Generated on Mon Jan 22 15:16:10 2007 for ProActive by  doxygen 1.5.1