001: /*
002: * This file or a portion of this file is licensed under the terms of
003: * the Globus Toolkit Public License, found in file GTPL, or at
004: * http://www.globus.org/toolkit/download/license.html. This notice must
005: * appear in redistributions of this file, with or without modification.
006: *
007: * Redistributions of this Software, with or without modification, must
008: * reproduce the GTPL in: (1) the Software, or (2) the Documentation or
009: * some other similar material which is provided with the Software (if
010: * any).
011: *
012: * Copyright 1999-2004 University of Chicago and The University of
013: * Southern California. All rights reserved.
014: */
015: package org.griphyn.cPlanner.partitioner;
016:
017: /**
018: * A bag implementation that just holds a particular value for the label key.
019: * This bag implements just contains one object, and a null value is associated
020: * by default with the label.
021: *
022: * @author Karan Vahi
023: * @version $Revision: 50 $
024: */
025:
026: public class LabelBag implements Bag {
027:
028: /**
029: * The default key that is associated with label.
030: */
031: public static String LABEL_KEY = "label";
032:
033: /**
034: * The key that designates the partition to which a node belongs to.
035: */
036: public static final String PARTITION_KEY = "partition";
037:
038: /**
039: * The value for the Label.
040: */
041: private Object mValue;
042:
043: /**
044: * The value for the partition key.
045: */
046: private Object mPartitionID;
047:
048: /**
049: * Sets the label key that is to be associated with the bag.
050: */
051: public static void setLabelKey(String key) {
052: LABEL_KEY = key;
053: }
054:
055: /**
056: * The default constructor.
057: */
058: public LabelBag() {
059: mValue = null;
060: mPartitionID = null;
061: }
062:
063: /**
064: * Returns an objects corresponding to the key passed.
065: *
066: * @param key the key corresponding to which the objects need to be returned.
067: *
068: * @return the object that is found corresponding to the key or null.
069: */
070: public Object get(Object key) {
071: return (key.equals(this .LABEL_KEY) ? mValue : key
072: .equals(this .PARTITION_KEY) ? mPartitionID : null);
073: }
074:
075: /**
076: * Adds an object to the underlying bag corresponding to a particular key.
077: *
078: * @param key the key with which the value has to be associated.
079: * @param value the value to be associated with the key.
080: */
081: public boolean add(Object key, Object value) {
082: boolean result = false;
083: if (key.equals(LABEL_KEY)) {
084: mValue = value;
085: result = true;
086: } else if (key.equals(PARTITION_KEY)) {
087: mPartitionID = value;
088: result = true;
089: }
090: return result;
091: }
092:
093: /**
094: * Returns true if the namespace contains a mapping for the specified key.
095: * More formally, returns true if and only if this map contains at a mapping
096: * for a key k such that (key==null ? k==null : key.equals(k)).
097: * (There can be at most one such mapping.)
098: *
099: * @param key The key that you want to search for in the bag.
100: */
101: public boolean containsKey(Object key) {
102: return key.equals(this .LABEL_KEY)
103: || key.equals(this .PARTITION_KEY);
104: }
105:
106: /**
107: * Returns a textual description of the Bag.
108: *
109: * @return String
110: */
111: public String toString() {
112: StringBuffer sb = new StringBuffer(32);
113: sb.append('{').append(mValue).append(',').append(mPartitionID)
114: .append('}');
115: return sb.toString();
116: }
117:
118: }
|