1 package net.sf.saxon.value; 2 3 import net.sf.saxon.expr.StaticProperty; 4 5 12 13 public final class Cardinality { 14 15 18 19 private Cardinality() {} 20 21 24 25 public static final boolean allowsMany(int cardinality) { 26 return (cardinality & StaticProperty.ALLOWS_MANY) != 0; 27 } 28 29 32 33 public static final boolean allowsZero(int cardinality) { 34 return (cardinality & StaticProperty.ALLOWS_ZERO) != 0; 35 } 36 37 44 45 public static final int union(int c1, int c2) { 46 int r = c1 | c2; 47 if (r == (StaticProperty.ALLOWS_MANY | 49 StaticProperty.ALLOWS_ZERO )) 50 r = StaticProperty.ALLOWS_ZERO_OR_MORE; 51 return r; 52 } 53 54 57 58 public static final int sum(int c1, int c2) { 59 if (allowsMany(c1) || allowsMany(c2)) { 60 return c1 | c2; 61 } 62 if (!allowsZero(c1) && !allowsZero(c2)) { 63 return StaticProperty.ALLOWS_ONE_OR_MORE; 64 } else { 65 return StaticProperty.ALLOWS_ZERO_OR_MORE; 66 } 67 } 68 69 77 78 public static final boolean subsumes(int c1, int c2) { 79 return (c1|c2)==c1; 80 } 81 82 85 86 public static final int add(int c1, int c2) { 87 if (c1==StaticProperty.EMPTY) { 88 return c2; 89 } 90 if (c2==StaticProperty.EMPTY) { 91 return c1; 92 } 93 boolean allowsZero = Cardinality.allowsZero(c1) && Cardinality.allowsZero(c2); 94 return StaticProperty.ALLOWS_ONE_OR_MORE | (allowsZero ? StaticProperty.ALLOWS_ZERO : 0); 95 } 96 97 100 101 public static final int multiply(int c1, int c2) { 102 if (c1==StaticProperty.EMPTY || c2==StaticProperty.EMPTY) { 103 return StaticProperty.EMPTY; 104 } 105 if (c2==StaticProperty.EXACTLY_ONE) { 106 return c1; 107 } 108 if (c1==StaticProperty.EXACTLY_ONE) { 109 return c2; 110 } 111 if (c1==StaticProperty.ALLOWS_ZERO_OR_ONE && c2==StaticProperty.ALLOWS_ZERO_OR_ONE) { 112 return StaticProperty.ALLOWS_ZERO_OR_ONE; 113 } 114 if (c1==StaticProperty.ALLOWS_ONE_OR_MORE && c2==StaticProperty.ALLOWS_ONE_OR_MORE) { 115 return StaticProperty.ALLOWS_ONE_OR_MORE; 116 } 117 return StaticProperty.ALLOWS_ZERO_OR_MORE; 118 } 119 120 123 124 public static String toString(int cardinality) { 125 switch (cardinality) { 126 case StaticProperty.ALLOWS_ZERO_OR_ONE: 127 return "zero or one"; 128 case StaticProperty.EXACTLY_ONE: 129 return "exactly one"; 130 case StaticProperty.ALLOWS_ZERO_OR_MORE: 131 return "zero or more"; 132 case StaticProperty.ALLOWS_ONE_OR_MORE: 133 return "one or more"; 134 case StaticProperty.EMPTY: 135 return "exactly zero"; 136 case StaticProperty.ALLOWS_MANY: 137 return "more than one"; 138 default: 139 return "code " + cardinality; 140 } 141 } 142 143 146 147 public static String getOccurrenceIndicator(int cardinality) { 148 switch (cardinality) { 149 case StaticProperty.ALLOWS_ZERO_OR_ONE: 150 return "?"; 151 case StaticProperty.EXACTLY_ONE: 152 return ""; 153 case StaticProperty.ALLOWS_ZERO_OR_MORE: 154 return "*"; 155 case StaticProperty.ALLOWS_ONE_OR_MORE: 156 return "+"; 157 default: throw new AssertionError ("unknown cardinality value"); 158 } 159 } 160 } 161 162 | Popular Tags |