jdk/src/share/classes/sun/security/x509/GeneralSubtrees.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2003 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.x509;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import sun.security.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Represent the GeneralSubtrees ASN.1 object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * The ASN.1 for this is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * GeneralSubtrees ::= SEQUENCE SIZE (1..MAX) OF GeneralSubtree
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @author Amit Kapoor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @author Hemma Prafullchandra
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @author Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
public class GeneralSubtrees implements Cloneable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private final List<GeneralSubtree> trees;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    // Private variables
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private static final int NAME_DIFF_TYPE = GeneralNameInterface.NAME_DIFF_TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static final int NAME_MATCH = GeneralNameInterface.NAME_MATCH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private static final int NAME_NARROWS = GeneralNameInterface.NAME_NARROWS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private static final int NAME_WIDENS = GeneralNameInterface.NAME_WIDENS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private static final int NAME_SAME_TYPE = GeneralNameInterface.NAME_SAME_TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * The default constructor for the class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    public GeneralSubtrees() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        trees = new ArrayList<GeneralSubtree>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private GeneralSubtrees(GeneralSubtrees source) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        trees = new ArrayList<GeneralSubtree>(source.trees);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * Create the object from the passed DER encoded form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * @param val the DER encoded form of the same.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public GeneralSubtrees(DerValue val) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        this();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        if (val.tag != DerValue.tag_Sequence) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            throw new IOException("Invalid encoding of GeneralSubtrees.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        while (val.data.available() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            DerValue opt = val.data.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            GeneralSubtree tree = new GeneralSubtree(opt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            add(tree);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public GeneralSubtree get(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        return trees.get(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public void remove(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        trees.remove(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public void add(GeneralSubtree tree) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        if (tree == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        trees.add(tree);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public boolean contains(GeneralSubtree tree) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (tree == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        return trees.contains(tree);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        return trees.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    public Iterator<GeneralSubtree> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        return trees.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public List<GeneralSubtree> trees() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        return trees;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        return new GeneralSubtrees(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * Return a printable string of the GeneralSubtree.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        String s = "   GeneralSubtrees:\n" + trees.toString() + "\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * Encode the GeneralSubtrees.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @params out the DerOutputStrean to encode this object to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    public void encode(DerOutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        DerOutputStream seq = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        for (int i = 0, n = size(); i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            get(i).encode(seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        out.write(DerValue.tag_Sequence, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * Compare two general subtrees by comparing the subtrees
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * of each.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @param other GeneralSubtrees to compare to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @returns true if match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        if (this == obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        if (obj instanceof GeneralSubtrees == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        GeneralSubtrees other = (GeneralSubtrees)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        return this.trees.equals(other.trees);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        return trees.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * Return the GeneralNameInterface form of the GeneralName in one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * the GeneralSubtrees.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @param ndx index of the GeneralSubtree from which to obtain the name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    private GeneralNameInterface getGeneralNameInterface(int ndx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        return getGeneralNameInterface(get(ndx));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    private static GeneralNameInterface getGeneralNameInterface(GeneralSubtree gs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        GeneralName gn = gs.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        GeneralNameInterface gni = gn.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        return gni;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * minimize this GeneralSubtrees by removing all redundant entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * Internal method used by intersect and reduce.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    private void minimize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        // Algorithm: compare each entry n to all subsequent entries in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        // the list: if any subsequent entry matches or widens entry n,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        // remove entry n. If any subsequent entries narrow entry n, remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        // the subsequent entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        for (int i = 0; i < size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            GeneralNameInterface current = getGeneralNameInterface(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            boolean remove1 = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            /* compare current to subsequent elements */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            for (int j = i + 1; j < size(); j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                GeneralNameInterface subsequent = getGeneralNameInterface(j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                switch (current.constrains(subsequent)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                    case GeneralNameInterface.NAME_DIFF_TYPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                        /* not comparable; different name types; keep checking */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                    case GeneralNameInterface.NAME_MATCH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                        /* delete one of the duplicates */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                        remove1 = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                    case GeneralNameInterface.NAME_NARROWS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                        /* subsequent narrows current */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                        /* remove narrower name (subsequent) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                        remove(j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                        j--; /* continue check with new subsequent */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                    case GeneralNameInterface.NAME_WIDENS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                        /* subsequent widens current */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                        /* remove narrower name current */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                        remove1 = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                    case GeneralNameInterface.NAME_SAME_TYPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                        /* keep both for now; keep checking */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            } /* end of this pass of subsequent elements */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            if (remove1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                remove(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                i--; /* check the new i value */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * create a subtree containing an instance of the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * name type that widens all other names of that type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * @params name GeneralNameInterface name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @returns GeneralSubtree containing widest name of that type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @throws RuntimeException on error (should not occur)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    private GeneralSubtree createWidestSubtree(GeneralNameInterface name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            GeneralName newName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            switch (name.getType()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            case GeneralNameInterface.NAME_ANY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                // Create new OtherName with same OID as baseName, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                // empty value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                ObjectIdentifier otherOID = ((OtherName)name).getOID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                newName = new GeneralName(new OtherName(otherOID, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            case GeneralNameInterface.NAME_RFC822:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                newName = new GeneralName(new RFC822Name(""));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            case GeneralNameInterface.NAME_DNS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                newName = new GeneralName(new DNSName(""));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            case GeneralNameInterface.NAME_X400:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                newName = new GeneralName(new X400Address((byte[])null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            case GeneralNameInterface.NAME_DIRECTORY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                newName = new GeneralName(new X500Name(""));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            case GeneralNameInterface.NAME_EDI:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                newName = new GeneralName(new EDIPartyName(""));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            case GeneralNameInterface.NAME_URI:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                newName = new GeneralName(new URIName(""));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            case GeneralNameInterface.NAME_IP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                newName = new GeneralName(new IPAddressName((byte[])null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            case GeneralNameInterface.NAME_OID:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                newName = new GeneralName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                    (new OIDName(new ObjectIdentifier((int[])null)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                throw new IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                    ("Unsupported GeneralNameInterface type: " + name.getType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            return new GeneralSubtree(newName, 0, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            throw new RuntimeException("Unexpected error: " + e, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * intersect this GeneralSubtrees with other.  This function
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * is used in merging permitted NameConstraints.  The operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * is performed as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * <li>If a name in other narrows all names of the same type in this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     *     the result will contain the narrower name and none of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *     names it narrows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * <li>If a name in other widens all names of the same type in this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     *     the result will not contain the wider name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * <li>If a name in other does not share the same subtree with any name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     *     of the same type in this, then the name is added to the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *     of GeneralSubtrees returned.  These names should be added to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     *     the list of names that are specifically excluded.  The reason
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     *     is that, if the intersection is empty, then no names of that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     *     type are permitted, and the only way to express this in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     *     NameConstraints is to include the name in excludedNames.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * <li>If a name in this has no name of the same type in other, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     *     the result contains the name in this.  No name of a given type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     *     means the name type is completely permitted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * <li>If a name in other has no name of the same type in this, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *     the result contains the name in other.  This means that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     *     the name is now constrained in some way, whereas before it was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     *     completely permitted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * @param other GeneralSubtrees to be intersected with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * @returns GeneralSubtrees to be merged with excluded; these are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     *          empty-valued name types corresponding to entries that were
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *          of the same type but did not share the same subtree between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *          this and other. Returns null if no such.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    public GeneralSubtrees intersect(GeneralSubtrees other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        if (other == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            throw new NullPointerException("other GeneralSubtrees must not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        GeneralSubtrees newThis = new GeneralSubtrees();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        GeneralSubtrees newExcluded = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        // Step 1: If this is empty, just add everything in other to this and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        // return no new excluded entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        if (size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            union(other);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        // Step 2: For ease of checking the subtrees, minimize them by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        // constructing versions that contain only the widest instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        // each type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        this.minimize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        other.minimize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        // Step 3: Check each entry in this to see whether we keep it or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        // remove it, and whether we add anything to newExcluded or newThis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        // We keep an entry in this unless it is narrowed by all entries in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        // other.  We add an entry to newExcluded if there is at least one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        // entry of the same nameType in other, but this entry does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        // not share the same subtree with any of the entries in other.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        // We add an entry from other to newThis if there is no name of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        // same type in this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        for (int i = 0; i < size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            GeneralNameInterface thisEntry = getGeneralNameInterface(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            boolean removeThisEntry = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            // Step 3a: If the widest name of this type in other narrows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            // thisEntry, remove thisEntry and add widest other to newThis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            // Simultaneously, check for situation where there is a name of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            // this type in other, but no name in other matches, narrows,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            // or widens thisEntry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            boolean sameType = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            for (int j = 0; j < other.size(); j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                GeneralSubtree otherEntryGS = other.get(j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                GeneralNameInterface otherEntry =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                    getGeneralNameInterface(otherEntryGS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                switch (thisEntry.constrains(otherEntry)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                    case NAME_NARROWS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                        remove(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                        i--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                        newThis.add(otherEntryGS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                        sameType = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                    case NAME_SAME_TYPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                        sameType = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                    case NAME_MATCH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                    case NAME_WIDENS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                        sameType = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                    case NAME_DIFF_TYPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            // Step 3b: If sameType is still true, we have the situation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            // where there was a name of the same type as thisEntry in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            // other, but no name in other widened, matched, or narrowed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            // thisEntry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            if (sameType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                // Step 3b.1: See if there are any entries in this and other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                // with this type that match, widen, or narrow each other.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                // If not, then we need to add a "widest subtree" of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                // type to excluded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                boolean intersection = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                for (int j = 0; j < size(); j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                    GeneralNameInterface thisAltEntry = getGeneralNameInterface(j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                    if (thisAltEntry.getType() == thisEntry.getType()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                        for (int k = 0; k < other.size(); k++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                            GeneralNameInterface othAltEntry =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                                other.getGeneralNameInterface(k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                            int constraintType =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                                thisAltEntry.constrains(othAltEntry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                            if (constraintType == NAME_MATCH ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                                constraintType == NAME_WIDENS ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                                constraintType == NAME_NARROWS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                                intersection = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                if (intersection == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                    if (newExcluded == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                        newExcluded = new GeneralSubtrees();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                    GeneralSubtree widestSubtree =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                         createWidestSubtree(thisEntry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                    if (!newExcluded.contains(widestSubtree)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                        newExcluded.add(widestSubtree);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                // Step 3b.2: Remove thisEntry from this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                remove(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                i--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        // Step 4: Add all entries in newThis to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        if (newThis.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            union(newThis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        // Step 5: Add all entries in other that do not have any entry of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        // same type in this to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        for (int i = 0; i < other.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            GeneralSubtree otherEntryGS = other.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            GeneralNameInterface otherEntry = getGeneralNameInterface(otherEntryGS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            boolean diffType = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            for (int j = 0; j < size(); j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                GeneralNameInterface thisEntry = getGeneralNameInterface(j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                switch (thisEntry.constrains(otherEntry)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                    case NAME_DIFF_TYPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                        diffType = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                        // continue to see if we find something later of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                        // same type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                    case NAME_NARROWS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                    case NAME_SAME_TYPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                    case NAME_MATCH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                    case NAME_WIDENS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                        diffType = false; // we found an entry of the same type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                        // break because we know we won't be adding it to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                        // this now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            if (diffType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                add(otherEntryGS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        // Step 6: Return the newExcluded GeneralSubtrees
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        return newExcluded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * construct union of this GeneralSubtrees with other.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * @param other GeneralSubtrees to be united with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    public void union(GeneralSubtrees other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        if (other != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            for (int i = 0, n = other.size(); i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                add(other.get(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            // Minimize this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            minimize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * reduce this GeneralSubtrees by contents of another.  This function
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * is used in merging excluded NameConstraints with permitted NameConstraints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * to obtain a minimal form of permitted NameConstraints.  It is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * optimization, and does not affect correctness of the results.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * @param excluded GeneralSubtrees
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    public void reduce(GeneralSubtrees excluded) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        if (excluded == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        for (int i = 0, n = excluded.size(); i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            GeneralNameInterface excludedName = excluded.getGeneralNameInterface(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            for (int j = 0; j < size(); j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                GeneralNameInterface permitted = getGeneralNameInterface(j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                switch (excludedName.constrains(permitted)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                case GeneralNameInterface.NAME_DIFF_TYPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                case GeneralNameInterface.NAME_MATCH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                    remove(j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                    j--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                case GeneralNameInterface.NAME_NARROWS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                    /* permitted narrows excluded */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                    remove(j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                    j--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                case GeneralNameInterface.NAME_WIDENS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                    /* permitted widens excluded */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                case GeneralNameInterface.NAME_SAME_TYPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            } /* end of this pass of permitted */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        } /* end of pass of excluded */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
}