📄 sum.java
字号:
/*
* Copyright (c) 2000-2005, University of Salford
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of the University of Salford nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package issrg.pba.credentials;
/**
* This is a sum of credentials.
*
* @see Property
*/
public class Sum extends DefaultCredentialBehaviour {
protected Credential [] sum;
protected Sum() {}
public Sum (Credential [] sum){
this.sum=normalise(sum);
}
/**
* Each member of the sum is indivisible. That is, if a+b=c, then c is either a, or b, not partially
* a, partially b.
*/
public boolean contains(Property p){
Property [] p1=(p instanceof Sum)? ((Sum)p).sum: new Property []{p};
return contains(sum, p1);
}
public boolean partOf(Property p){
return contains(new Property[]{p}, sum);
}
private static boolean contains(Property [] s1, Property [] s2){
if (s1==null){
s1 = new Property[0];
}
if (s2==null){
s2 = new Property[0];
}
if (s1.length==0 && s2.length==0){ // even an empty array contains an empty array
return true;
}
boolean [] r = new boolean [s2.length];
for (int i=0; i<s1.length; i++){
if (s1[i]!=null){
boolean b=true;
for (int j=0; j<s2.length; j++){
if (!r[j]){
r[j]=s1[i].contains(s2[j]);
}
b &= r[j]; // collecting the result; if b is true after the loop, then all of r[j] are true
// already, and therefore the s2 is already contained within the s1
}
if (b){
return true; // otherwise continue to loop through the s1
}
}
}
return false;
}
public Credential intersect(Credential c){
Credential r = new Sum(new Credential[0]);
if (c instanceof Sum){
Credential [] s1= ((Sum)c).sum;
for (int i=0; i<s1.length; i++){
r.join(intersect(s1[i]));
}
}else{
for (int i=0; i<sum.length; i++){
r.join(sum[i].intersect(c));
}
}
return r;
}
public Credential join(Credential c){
if (c==null){
return this;
}
Credential [] s1 = (c instanceof Sum)? ((Sum)c).sum: new Credential[]{c};
Credential [] r = new Credential[sum.length+s1.length];
System.arraycopy(s1, 0, r, 0, s1.length);
System.arraycopy(sum, 0, r, s1.length, sum.length);
return new Sum(r);
}
/**
* This method returns a "normalised" set of credentials. It will check if any of the
* credentials in the array contains any other credentials in the set and will eliminate
* those redundant elements.
*
* @param sum is the set to normalise
*
* @return the normalised set of credential
*/
private static Credential [] normalise(Credential [] sum){
int o = sum.length;
Credential [] s1 = new Credential[o];
System.arraycopy(sum, 0, s1, 0, o);
for (int i=0; i<s1.length; i++){
if (s1[i]==null) continue;
for (int j=0; j<s1.length; j++){
if (i==j) continue;
if (s1[j]!=null && s1[i].contains(s1[j])){
s1[j]=null;
o--;
}
}
}
Credential [] r = new Credential [o];
for (int i=s1.length; i-->0;){
if (s1[i]==null) continue;
o--;
r[o]=s1[i];
}
return r;
}
public String toString(){
if (sum==null){
return null;
}
StringBuffer sb = new StringBuffer("s{");
for (int i=0; i<sum.length; i++){
if (i>0){
sb.append(", ");
}
sb.append(sum[i]==null? "null" : sum[i].toString());
}
return sb.append("}").toString();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -