001: /*=============================================================================
002: * Copyright Texas Instruments 2002. All Rights Reserved.
003: *
004: * This program is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package ti.chimera.registry;
020:
021: import ti.exceptions.ProgrammingErrorException;
022:
023: /**
024: * A node contract that is composed of multiple other contracts using the
025: * and (&&) operator.
026: *
027: * @author ;Rob Clark;a0873619;San Diego;;
028: * @version 0.1
029: */
030: public class AndNodeContract implements NodeContract {
031: private final NodeContract[] ncs;
032:
033: /**
034: * Class Constructor, construct a node contract that is the AND of
035: * <code>a</code> and <code>b</code>
036: *
037: * @param a node contract
038: * @param b node contract
039: */
040: public AndNodeContract(NodeContract a, NodeContract b) {
041: this (new NodeContract[] { a, b });
042: }
043:
044: /**
045: * Class Constructor, construct a node contract that is the AND of
046: * all the elements in the <code>ncs</code> array.
047: *
048: * @param ncs node contracts
049: */
050: public AndNodeContract(NodeContract[] ncs) {
051: if ((ncs == null) || (ncs.length == 0))
052: throw new ProgrammingErrorException("can't AND nothing!!");
053: this .ncs = ncs;
054: }
055:
056: /**
057: * Determine if the specified <code>value</code> meets this contract.
058: *
059: * @param value the value to check
060: * @return <code>true</code> if meets contract
061: */
062: public boolean accepts(Object value) {
063: boolean accepted = true;
064:
065: for (int i = 0; i < ncs.length && accepted; i++)
066: accepted = ncs[i].accepts(value);
067:
068: return accepted;
069: }
070:
071: /**
072: * The contract implementation should overload <code>toString</code> so
073: * the contract can be displayed to the user in a sane format, for use
074: * in error messages, etc.
075: */
076: public String toString() {
077: StringBuffer sb = new StringBuffer();
078:
079: sb.append("(");
080: sb.append(ncs[0].toString());
081:
082: for (int i = 1; i < ncs.length; i++) {
083: sb.append(" && ");
084: sb.append(ncs[i].toString());
085: }
086:
087: sb.append(")");
088:
089: return sb.toString();
090: }
091: }
092:
093: /*
094: * Local Variables:
095: * tab-width: 2
096: * indent-tabs-mode: nil
097: * mode: java
098: * c-indentation-style: java
099: * c-basic-offset: 2
100: * eval: (c-set-offset 'substatement-open '0)
101: * eval: (c-set-offset 'case-label '+)
102: * eval: (c-set-offset 'inclass '+)
103: * eval: (c-set-offset 'inline-open '0)
104: * End:
105: */
|