Source code: com/sun/xacml/cond/EvaluationResult.java
1
2 /*
3 * @(#)EvaluationResult.java
4 *
5 * Copyright 2003-2004 Sun Microsystems, Inc. All Rights Reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 *
10 * 1. Redistribution of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
12 *
13 * 2. Redistribution in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * Neither the name of Sun Microsystems, Inc. or the names of contributors may
18 * be used to endorse or promote products derived from this software without
19 * specific prior written permission.
20 *
21 * This software is provided "AS IS," without a warranty of any kind. ALL
22 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
23 * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
24 * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN")
25 * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
26 * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
27 * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
28 * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
29 * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
30 * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
31 * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
32 *
33 * You acknowledge that this software is not designed or intended for use in
34 * the design, construction, operation or maintenance of any nuclear facility.
35 */
36
37 package com.sun.xacml.cond;
38
39 import com.sun.xacml.attr.AttributeValue;
40 import com.sun.xacml.attr.BooleanAttribute;
41
42 import com.sun.xacml.ctx.Status;
43
44
45 /**
46 * This is used in cases where a normal result is some AttributeValue, but
47 * if an attribute couldn't be resolved (or some other problem occurred),
48 * then a Status object needs to be returned instead. This is used instead of
49 * throwing an exception for performance, but mainly because failure to resolve
50 * an attribute is not an error case for the code, merely for the evaluation,
51 * and represents normal operation. Separate exception types will be added
52 * later to represent errors in pdp operation.
53 *
54 * @since 1.0
55 * @author Seth Proctor
56 */
57 public class EvaluationResult
58 {
59
60 //
61 private boolean wasInd;
62 private AttributeValue value;
63 private Status status;
64
65 /**
66 * Single instances of EvaluationResults with false and true
67 * BooleanAttributes in them. This avoids the need to create
68 * new objects when performing boolean operations, which we
69 * do a lot of.
70 */
71 private static EvaluationResult falseBooleanResult;
72 private static EvaluationResult trueBooleanResult;
73
74 /**
75 * Constructor that creates an <code>EvaluationResult</code> containing
76 * a single <code>AttributeValue</code>
77 *
78 * @param value the attribute value
79 */
80 public EvaluationResult(AttributeValue value) {
81 wasInd = false;
82 this.value = value;
83 this.status = null;
84 }
85
86 /**
87 * Constructor that creates an <code>EvaluationResult</code> of
88 * Indeterminate, including Status data.
89 *
90 * @param status the error information
91 */
92 public EvaluationResult(Status status) {
93 wasInd = true;
94 this.value = null;
95 this.status = status;
96 }
97
98 /**
99 * Returns true if the result was indeterminate
100 *
101 * @return true if there was an error
102 */
103 public boolean indeterminate() {
104 return wasInd;
105 }
106
107 /**
108 * Returns the attribute value, or null if there was an error
109 *
110 * @return the attribute value or null
111 */
112 public AttributeValue getAttributeValue() {
113 return value;
114 }
115
116 /**
117 * Returns the status if there was an error, or null it no error occurred
118 *
119 * @return the status or null
120 */
121 public Status getStatus() {
122 return status;
123 }
124
125 /**
126 * Returns an <code>EvaluationResult</code> that represents
127 * the boolean value provided.
128 *
129 * @param value a boolean representing the desired value
130 * @return an <code>EvaluationResult</code> representing the
131 * appropriate value
132 */
133 public static EvaluationResult getInstance(boolean value) {
134 if (value)
135 return getTrueInstance();
136 else
137 return getFalseInstance();
138 }
139
140 /**
141 * Returns an <code>EvaluationResult</code> that represents
142 * a false value.
143 *
144 * @return an <code>EvaluationResult</code> representing a
145 * false value
146 */
147 public static EvaluationResult getFalseInstance() {
148 if (falseBooleanResult == null) {
149 falseBooleanResult =
150 new EvaluationResult(BooleanAttribute.getFalseInstance());
151 }
152 return falseBooleanResult;
153 }
154
155 /**
156 * Returns an <code>EvaluationResult</code> that represents
157 * a true value.
158 *
159 * @return an <code>EvaluationResult</code> representing a
160 * true value
161 */
162 public static EvaluationResult getTrueInstance() {
163 if (trueBooleanResult == null) {
164 trueBooleanResult =
165 new EvaluationResult(BooleanAttribute.getTrueInstance());
166 }
167 return trueBooleanResult;
168 }
169 }