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