jdk/src/java.base/share/classes/java/lang/module/ModuleDescriptor.java
author sdrach
Tue, 19 Jul 2016 10:05:22 -0700
changeset 39758 28c2c63fc09f
parent 39050 9de41b79ec7e
child 41817 b90ad1de93ea
permissions -rw-r--r--
8157524: Revert JarFile methods "entries" and "stream" to Java 8 behavior Reviewed-by: alanb, psandoz, redestad
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     2
 * Copyright (c) 2009, 2016, Oracle and/or its affiliates. All rights reserved.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * accompanied this code).
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    23
 * questions.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    24
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    25
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    26
package java.lang.module;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    28
import java.io.InputStream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
import java.io.IOException;
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    30
import java.io.PrintStream;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
import java.io.UncheckedIOException;
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    32
import java.net.URI;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import java.nio.ByteBuffer;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.util.ArrayList;
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    35
import java.util.Collection;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.util.Collections;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import java.util.EnumSet;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
import java.util.HashMap;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
import java.util.HashSet;
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    40
import java.util.LinkedHashSet;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
import java.util.List;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
import java.util.Map;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
import java.util.Objects;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
import java.util.Optional;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
import java.util.Set;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
import java.util.function.Supplier;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
import static jdk.internal.module.Checks.*;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
import static java.util.Objects.*;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
import jdk.internal.module.Checks;
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    52
import jdk.internal.module.ModuleHashes;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
/**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
 * A module descriptor.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
 * <p> A {@code ModuleDescriptor} is typically created from the binary form
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
 * of a module declaration. The associated {@link ModuleDescriptor.Builder}
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
 * class can also be used to create a {@code ModuleDescriptor} from its
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
 * components. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
 * <p> {@code ModuleDescriptor} objects are immutable and safe for use by
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
 * multiple concurrent threads.</p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
 * @since 9
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
 * @see java.lang.reflect.Module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
public class ModuleDescriptor
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
    implements Comparable<ModuleDescriptor>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
{
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
     * <p> A dependence upon a module </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
     * @see ModuleDescriptor#requires()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
     * @since 9
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
    public final static class Requires
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
        implements Comparable<Requires>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
    {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
         * A modifier on a module dependence.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
         * @since 9
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
        public static enum Modifier {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
            /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
             * The dependence causes any module which depends on the <i>current
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
             * module</i> to have an implicitly declared dependence on the module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
             * named by the {@code Requires}.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
             */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
            PUBLIC,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
            /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
             * The dependence was not explicitly or implicitly declared in the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
             * source of the module declaration.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
             */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
            SYNTHETIC,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
            /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
             * The dependence was implicitly declared in the source of the module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
             * declaration.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
             */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
            MANDATED;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
        private final Set<Modifier> mods;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
        private final String name;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
        private Requires(Set<Modifier> ms, String mn) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
            this(ms, mn, true);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
        private Requires(Set<Modifier> ms, String mn, boolean check) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
            if (ms == null || ms.isEmpty()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
                mods = Collections.emptySet();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
            } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
                mods = check ? Collections.unmodifiableSet(EnumSet.copyOf(ms))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
                             : ms;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
            this.name = check ? requireModuleName(mn) : mn;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
         * Returns the set of modifiers.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
         * @return A possibly-empty unmodifiable set of modifiers
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   133
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
        public Set<Modifier> modifiers() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
            return mods;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   138
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   139
         * Return the module name.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   140
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   141
         * @return The module name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   142
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   143
        public String name() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   144
            return name;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   145
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   146
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   147
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   148
         * Compares this module dependence to another.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   149
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   150
         * <p> Two {@code Requires} objects are compared by comparing their
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
         * module name lexicographically.  Where the module names are equal then
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   152
         * the sets of modifiers are compared based on a value computed from the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   153
         * ordinal of each modifier. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   154
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   155
         * @return A negative integer, zero, or a positive integer if this module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   156
         *         dependence is less than, equal to, or greater than the given
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   157
         *         module dependence
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   158
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   159
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   160
        public int compareTo(Requires that) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   161
            int c = this.name().compareTo(that.name());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   162
            if (c != 0)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   163
                return c;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   164
            // same name, compare by modifiers
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   165
            return Long.compare(this.modsValue(), that.modsValue());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   166
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   167
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   168
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   169
         * Return a value for the modifiers to allow sets of modifiers to be
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   170
         * compared.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   171
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   172
        private long modsValue() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   173
            long value = 0;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   174
            for (Modifier m : mods) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   175
                value += 1 << m.ordinal();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   176
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   177
            return value;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   178
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   179
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   180
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   181
         * Tests this module dependence for equality with the given object.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   182
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   183
         * <p> If the given object is not a {@code Requires} then this method
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   184
         * returns {@code false}. Two module dependence objects are equal if
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   185
         * the module names are equal and set of modifiers are equal. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   186
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   187
         * <p> This method satisfies the general contract of the {@link
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   188
         * java.lang.Object#equals(Object) Object.equals} method. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   189
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   190
         * @param   ob
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   191
         *          the object to which this object is to be compared
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   192
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   193
         * @return  {@code true} if, and only if, the given object is a module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   194
         *          dependence that is equal to this module dependence
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   195
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   196
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   197
        public boolean equals(Object ob) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   198
            if (!(ob instanceof Requires))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   199
                return false;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   200
            Requires that = (Requires)ob;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   201
            return (name.equals(that.name) && mods.equals(that.mods));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   202
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   203
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   204
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   205
         * Computes a hash code for this module dependence.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   206
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   207
         * <p> The hash code is based upon the module name and modifiers. It
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   208
         * satisfies the general contract of the {@link Object#hashCode
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   209
         * Object.hashCode} method. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   210
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   211
         * @return The hash-code value for this module dependence
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   212
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   213
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   214
        public int hashCode() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   215
            return name.hashCode() * 43 + mods.hashCode();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   216
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   217
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   218
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   219
         * Returns a string describing module dependence.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   220
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   221
         * @return A string describing module dependence
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   222
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   223
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   224
        public String toString() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   225
            return Dependence.toString(mods, name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   226
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   227
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   228
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   229
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   230

9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   231
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   232
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   233
     * <p> A module export, may be qualified or unqualified. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   234
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   235
     * @see ModuleDescriptor#exports()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   236
     * @since 9
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   237
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   238
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   239
    public final static class Exports {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   240
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   241
        private final String source;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   242
        private final Set<String> targets;  // empty if unqualified export
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   243
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   244
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   245
         * Constructs a qualified export.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   246
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   247
        private Exports(String source, Set<String> targets) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   248
            this(source, targets, true);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   249
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   250
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   251
        private Exports(String source, Set<String> targets, boolean check) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   252
            this.source = check ? requirePackageName(source) : source;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   253
            targets = check ? Collections.unmodifiableSet(new HashSet<>(targets))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   254
                            : Collections.unmodifiableSet(targets);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   255
            if (targets.isEmpty())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   256
                throw new IllegalArgumentException("Empty target set");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   257
            if (check)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   258
                targets.stream().forEach(Checks::requireModuleName);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   259
            this.targets = targets;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   260
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   261
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   262
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   263
         * Constructs an unqualified export.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   264
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   265
        private Exports(String source) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   266
            this(source, true);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   267
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   268
        private Exports(String source, boolean check) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   269
            this.source = check ? requirePackageName(source) : source;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   270
            this.targets = Collections.emptySet();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   271
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   272
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   273
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   274
         * Returns {@code true} if this is a qualified export.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   275
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   276
         * @return {@code true} if this is a qualified export
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   277
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   278
        public boolean isQualified() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   279
            return !targets.isEmpty();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   280
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   281
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   282
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   283
         * Returns the package name.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   284
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   285
         * @return The package name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   286
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   287
        public String source() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   288
            return source;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   289
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   290
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   291
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   292
         * For a qualified export, returns the non-empty and immutable set
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   293
         * of the module names to which the package is exported. For an
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   294
         * unqualified export, returns an empty set.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   295
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   296
         * @return The set of target module names or for an unqualified
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   297
         *         export, an empty set
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   298
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   299
        public Set<String> targets() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   300
            return targets;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   301
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   302
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   303
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   304
         * Computes a hash code for this module export.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   305
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   306
         * <p> The hash code is based upon the package name, and for a
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   307
         * qualified export, the set of modules names to which the package
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   308
         * is exported. It satisfies the general contract of the {@link
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   309
         * Object#hashCode Object.hashCode} method.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   310
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   311
         * @return The hash-code value for this module export
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   312
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   313
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   314
        public int hashCode() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   315
            return hash(source, targets);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   316
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   317
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   318
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   319
         * Tests this module export for equality with the given object.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   320
         *
37880
60ec48925dc6 8156661: Handful of typos in javadoc
igerasim
parents: 37779
diff changeset
   321
         * <p> If the given object is not an {@code Exports} then this method
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   322
         * returns {@code false}. Two module exports objects are equal if the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   323
         * package names are equal and the set of target module names is equal.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   324
         * </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   325
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   326
         * <p> This method satisfies the general contract of the {@link
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   327
         * java.lang.Object#equals(Object) Object.equals} method. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   328
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   329
         * @param   ob
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   330
         *          the object to which this object is to be compared
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   331
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   332
         * @return  {@code true} if, and only if, the given object is a module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   333
         *          dependence that is equal to this module dependence
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   334
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   335
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   336
        public boolean equals(Object ob) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   337
            if (!(ob instanceof Exports))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   338
                return false;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   339
            Exports other = (Exports)ob;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   340
            return Objects.equals(this.source, other.source) &&
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   341
                Objects.equals(this.targets, other.targets);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   342
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   343
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   344
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   345
         * Returns a string describing module export.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   346
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   347
         * @return A string describing module export
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   348
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   349
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   350
        public String toString() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   351
            if (targets.isEmpty())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   352
                return source;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   353
            else
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   354
                return source + " to " + targets;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   355
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   356
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   357
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   358
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   359

9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   360
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   361
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   362
     * <p> A service that a module provides one or more implementations of. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   363
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   364
     * @see ModuleDescriptor#provides()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   365
     * @since 9
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   366
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   367
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   368
    public final static class Provides {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   369
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   370
        private final String service;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   371
        private final Set<String> providers;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   372
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   373
        private Provides(String service, Set<String> providers) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   374
            this(service, providers, true);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   375
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   376
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   377
        private Provides(String service, Set<String> providers, boolean check) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   378
            this.service = check ? requireServiceTypeName(service) : service;
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   379
            providers = check
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   380
                ? Collections.unmodifiableSet(new LinkedHashSet<>(providers))
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   381
                : Collections.unmodifiableSet(providers);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   382
            if (providers.isEmpty())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   383
                throw new IllegalArgumentException("Empty providers set");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   384
            if (check)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   385
                providers.forEach(Checks::requireServiceProviderName);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   386
            this.providers = providers;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   387
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   388
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   389
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   390
         * Returns the fully qualified class name of the service type.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   391
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   392
         * @return The fully qualified class name of the service type.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   393
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   394
        public String service() { return service; }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   395
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   396
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   397
         * Returns the set of the fully qualified class names of the providers.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   398
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   399
         * @return A non-empty and unmodifiable set of the fully qualified class
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   400
         *         names of the providers.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   401
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   402
        public Set<String> providers() { return providers; }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   403
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   404
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   405
         * Computes a hash code for this provides.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   406
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   407
         * <p> The hash code is based upon the service type and the set of
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   408
         * providers. It satisfies the general contract of the {@link
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   409
         * Object#hashCode Object.hashCode} method. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   410
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   411
         * @return The hash-code value for this module provides
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   412
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   413
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   414
        public int hashCode() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   415
            return hash(service, providers);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   416
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   417
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   418
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   419
         * Tests this provides for equality with the given object.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   420
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   421
         * <p> If the given object is not a {@code Provides} then this method
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   422
         * returns {@code false}. Two {@code Provides} objects are equal if the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   423
         * service type is equal and the set of providers is equal. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   424
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   425
         * <p> This method satisfies the general contract of the {@link
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   426
         * java.lang.Object#equals(Object) Object.equals} method. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   427
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   428
         * @param   ob
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   429
         *          the object to which this object is to be compared
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   430
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   431
         * @return  {@code true} if, and only if, the given object is a
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   432
         *          {@code Provides} that is equal to this {@code Provides}
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   433
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   434
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   435
        public boolean equals(Object ob) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   436
            if (!(ob instanceof Provides))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   437
                return false;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   438
            Provides other = (Provides)ob;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   439
            return Objects.equals(this.service, other.service) &&
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   440
                    Objects.equals(this.providers, other.providers);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   441
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   442
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   443
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   444
         * Returns a string describing this provides.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   445
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   446
         * @return A string describing this provides
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   447
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   448
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   449
        public String toString() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   450
            return service + " with " + providers;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   451
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   452
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   453
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   454
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   455

9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   456
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   457
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   458
     * A module's version string.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   459
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   460
     * <p> A version string has three components: The version number itself, an
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   461
     * optional pre-release version, and an optional build version.  Each
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   462
     * component is sequence of tokens; each token is either a non-negative
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   463
     * integer or a string.  Tokens are separated by the punctuation characters
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   464
     * {@code '.'}, {@code '-'}, or {@code '+'}, or by transitions from a
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   465
     * sequence of digits to a sequence of characters that are neither digits
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   466
     * nor punctuation characters, or vice versa.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   467
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   468
     * <ul>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   469
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   470
     *   <li> The <i>version number</i> is a sequence of tokens separated by
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   471
     *   {@code '.'} characters, terminated by the first {@code '-'} or {@code
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   472
     *   '+'} character. </li>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   473
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   474
     *   <li> The <i>pre-release version</i> is a sequence of tokens separated
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   475
     *   by {@code '.'} or {@code '-'} characters, terminated by the first
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   476
     *   {@code '+'} character. </li>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   477
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   478
     *   <li> The <i>build version</i> is a sequence of tokens separated by
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   479
     *   {@code '.'}, {@code '-'}, or {@code '+'} characters.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   480
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   481
     * </ul>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   482
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   483
     * <p> When comparing two version strings, the elements of their
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   484
     * corresponding components are compared in pointwise fashion.  If one
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   485
     * component is longer than the other, but otherwise equal to it, then the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   486
     * first component is considered the greater of the two; otherwise, if two
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   487
     * corresponding elements are integers then they are compared as such;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   488
     * otherwise, at least one of the elements is a string, so the other is
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   489
     * converted into a string if it is an integer and the two are compared
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   490
     * lexicographically.  Trailing integer elements with the value zero are
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   491
     * ignored.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   492
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   493
     * <p> Given two version strings, if their version numbers differ then the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   494
     * result of comparing them is the result of comparing their version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   495
     * numbers; otherwise, if one of them has a pre-release version but the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   496
     * other does not then the first is considered to precede the second,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   497
     * otherwise the result of comparing them is the result of comparing their
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   498
     * pre-release versions; otherwise, the result of comparing them is the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   499
     * result of comparing their build versions.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   500
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   501
     * @see ModuleDescriptor#version()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   502
     * @since 9
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   503
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   504
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   505
    public final static class Version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   506
        implements Comparable<Version>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   507
    {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   508
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   509
        private final String version;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   510
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   511
        // If Java had disjunctive types then we'd write List<Integer|String> here
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   512
        //
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   513
        private final List<Object> sequence;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   514
        private final List<Object> pre;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   515
        private final List<Object> build;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   516
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   517
        // Take a numeric token starting at position i
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   518
        // Append it to the given list
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   519
        // Return the index of the first character not taken
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   520
        // Requires: s.charAt(i) is (decimal) numeric
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   521
        //
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   522
        private static int takeNumber(String s, int i, List<Object> acc) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   523
            char c = s.charAt(i);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   524
            int d = (c - '0');
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   525
            int n = s.length();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   526
            while (++i < n) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   527
                c = s.charAt(i);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   528
                if (c >= '0' && c <= '9') {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   529
                    d = d * 10 + (c - '0');
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   530
                    continue;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   531
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   532
                break;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   533
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   534
            acc.add(d);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   535
            return i;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   536
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   537
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   538
        // Take a string token starting at position i
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   539
        // Append it to the given list
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   540
        // Return the index of the first character not taken
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   541
        // Requires: s.charAt(i) is not '.'
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   542
        //
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   543
        private static int takeString(String s, int i, List<Object> acc) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   544
            int b = i;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   545
            int n = s.length();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   546
            while (++i < n) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   547
                char c = s.charAt(i);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   548
                if (c != '.' && c != '-' && c != '+' && !(c >= '0' && c <= '9'))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   549
                    continue;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   550
                break;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   551
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   552
            acc.add(s.substring(b, i));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   553
            return i;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   554
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   555
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   556
        // Syntax: tok+ ( '-' tok+)? ( '+' tok+)?
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   557
        // First token string is sequence, second is pre, third is build
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   558
        // Tokens are separated by '.' or '-', or by changes between alpha & numeric
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   559
        // Numeric tokens are compared as decimal integers
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   560
        // Non-numeric tokens are compared lexicographically
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   561
        // A version with a non-empty pre is less than a version with same seq but no pre
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   562
        // Tokens in build may contain '-' and '+'
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   563
        //
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   564
        private Version(String v) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   565
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   566
            if (v == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   567
                throw new IllegalArgumentException("Null version string");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   568
            int n = v.length();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   569
            if (n == 0)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   570
                throw new IllegalArgumentException("Empty version string");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   571
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   572
            int i = 0;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   573
            char c = v.charAt(i);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   574
            if (!(c >= '0' && c <= '9'))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   575
                throw new IllegalArgumentException(v
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   576
                                                   + ": Version string does not start"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   577
                                                   + " with a number");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   578
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   579
            List<Object> sequence = new ArrayList<>(4);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   580
            List<Object> pre = new ArrayList<>(2);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   581
            List<Object> build = new ArrayList<>(2);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   582
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   583
            i = takeNumber(v, i, sequence);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   584
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   585
            while (i < n) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   586
                c = v.charAt(i);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   587
                if (c == '.') {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   588
                    i++;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   589
                    continue;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   590
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   591
                if (c == '-' || c == '+') {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   592
                    i++;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   593
                    break;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   594
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   595
                if (c >= '0' && c <= '9')
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   596
                    i = takeNumber(v, i, sequence);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   597
                else
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   598
                    i = takeString(v, i, sequence);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   599
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   600
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   601
            if (c == '-' && i >= n)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   602
                throw new IllegalArgumentException(v + ": Empty pre-release");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   603
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   604
            while (i < n) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   605
                c = v.charAt(i);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   606
                if (c >= '0' && c <= '9')
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   607
                    i = takeNumber(v, i, pre);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   608
                else
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   609
                    i = takeString(v, i, pre);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   610
                if (i >= n)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   611
                    break;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   612
                c = v.charAt(i);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   613
                if (c == '.' || c == '-') {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   614
                    i++;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   615
                    continue;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   616
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   617
                if (c == '+') {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   618
                    i++;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   619
                    break;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   620
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   621
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   622
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   623
            if (c == '+' && i >= n)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   624
                throw new IllegalArgumentException(v + ": Empty pre-release");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   625
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   626
            while (i < n) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   627
                c = v.charAt(i);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   628
                if (c >= '0' && c <= '9')
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   629
                    i = takeNumber(v, i, build);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   630
                else
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   631
                    i = takeString(v, i, build);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   632
                if (i >= n)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   633
                    break;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   634
                c = v.charAt(i);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   635
                if (c == '.' || c == '-' || c == '+') {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   636
                    i++;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   637
                    continue;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   638
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   639
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   640
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   641
            this.version = v;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   642
            this.sequence = sequence;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   643
            this.pre = pre;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   644
            this.build = build;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   645
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   646
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   647
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   648
         * Parses the given string as a version string.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   649
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   650
         * @param  v
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   651
         *         The string to parse
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   652
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   653
         * @return The resulting {@code Version}
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   654
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   655
         * @throws IllegalArgumentException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   656
         *         If {@code v} is {@code null}, an empty string, or cannot be
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   657
         *         parsed as a version string
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   658
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   659
        public static Version parse(String v) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   660
            return new Version(v);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   661
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   662
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   663
        @SuppressWarnings("unchecked")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   664
        private int cmp(Object o1, Object o2) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   665
            return ((Comparable)o1).compareTo(o2);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   666
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   667
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   668
        private int compareTokens(List<Object> ts1, List<Object> ts2) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   669
            int n = Math.min(ts1.size(), ts2.size());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   670
            for (int i = 0; i < n; i++) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   671
                Object o1 = ts1.get(i);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   672
                Object o2 = ts2.get(i);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   673
                if ((o1 instanceof Integer && o2 instanceof Integer)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   674
                    || (o1 instanceof String && o2 instanceof String))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   675
                {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   676
                    int c = cmp(o1, o2);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   677
                    if (c == 0)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   678
                        continue;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   679
                    return c;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   680
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   681
                // Types differ, so convert number to string form
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   682
                int c = o1.toString().compareTo(o2.toString());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   683
                if (c == 0)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   684
                    continue;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   685
                return c;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   686
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   687
            List<Object> rest = ts1.size() > ts2.size() ? ts1 : ts2;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   688
            int e = rest.size();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   689
            for (int i = n; i < e; i++) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   690
                Object o = rest.get(i);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   691
                if (o instanceof Integer && ((Integer)o) == 0)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   692
                    continue;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   693
                return ts1.size() - ts2.size();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   694
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   695
            return 0;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   696
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   697
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   698
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   699
         * Compares this module version to another module version. Module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   700
         * versions are compared as described in the class description.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   701
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   702
         * @param that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   703
         *        The module version to compare
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   704
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   705
         * @return A negative integer, zero, or a positive integer as this
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   706
         *         module version is less than, equal to, or greater than the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   707
         *         given module version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   708
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   709
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   710
        public int compareTo(Version that) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   711
            int c = compareTokens(this.sequence, that.sequence);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   712
            if (c != 0) return c;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   713
            if (this.pre.isEmpty()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   714
                if (!that.pre.isEmpty()) return +1;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   715
            } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   716
                if (that.pre.isEmpty()) return -1;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   717
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   718
            c = compareTokens(this.pre, that.pre);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   719
            if (c != 0) return c;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   720
            return compareTokens(this.build, that.build);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   721
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   722
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   723
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   724
         * Tests this module version for equality with the given object.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   725
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   726
         * <p> If the given object is not a {@code Version} then this method
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   727
         * returns {@code false}. Two module version are equal if their
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   728
         * corresponding components are equal. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   729
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   730
         * <p> This method satisfies the general contract of the {@link
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   731
         * java.lang.Object#equals(Object) Object.equals} method. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   732
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   733
         * @param   ob
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   734
         *          the object to which this object is to be compared
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   735
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   736
         * @return  {@code true} if, and only if, the given object is a module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   737
         *          reference that is equal to this module reference
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   738
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   739
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   740
        public boolean equals(Object ob) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   741
            if (!(ob instanceof Version))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   742
                return false;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   743
            return compareTo((Version)ob) == 0;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   744
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   745
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   746
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   747
         * Computes a hash code for this module version.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   748
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   749
         * <p> The hash code is based upon the components of the version and
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   750
         * satisfies the general contract of the {@link Object#hashCode
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   751
         * Object.hashCode} method. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   752
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   753
         * @return The hash-code value for this module version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   754
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   755
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   756
        public int hashCode() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   757
            return version.hashCode();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   758
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   759
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   760
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   761
         * Returns the string from which this version was parsed.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   762
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   763
         * @return The string from which this version was parsed.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   764
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   765
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   766
        public String toString() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   767
            return version;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   768
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   769
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   770
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   771
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   772

9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   773
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   774
    // From module declarations
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   775
    private final String name;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   776
    private final Set<Requires> requires;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   777
    private final Set<Exports> exports;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   778
    private final Set<String> uses;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   779
    private final Map<String, Provides> provides;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   780
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   781
    // Indicates if synthesised for a JAR file found on the module path
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   782
    private final boolean automatic;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   783
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   784
    // Not generated from a module-info.java
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   785
    private final boolean synthetic;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   786
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   787
    // "Extended" information, added post-compilation by tools
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   788
    private final Version version;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   789
    private final String mainClass;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   790
    private final String osName;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   791
    private final String osArch;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   792
    private final String osVersion;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   793
    private final Set<String> packages;
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   794
    private final ModuleHashes hashes;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   795
38888
dd584fbea6a2 8159334: ModuleDescriptor retains overlapping sets for all and concealed packages
redestad
parents: 37880
diff changeset
   796
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   797
    private ModuleDescriptor(String name,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   798
                             boolean automatic,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   799
                             boolean synthetic,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   800
                             Map<String, Requires> requires,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   801
                             Set<String> uses,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   802
                             Map<String, Exports> exports,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   803
                             Map<String, Provides> provides,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   804
                             Version version,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   805
                             String mainClass,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   806
                             String osName,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   807
                             String osArch,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   808
                             String osVersion,
38888
dd584fbea6a2 8159334: ModuleDescriptor retains overlapping sets for all and concealed packages
redestad
parents: 37880
diff changeset
   809
                             Set<String> packages,
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   810
                             ModuleHashes hashes)
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   811
    {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   812
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   813
        this.name = name;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   814
        this.automatic = automatic;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   815
        this.synthetic = synthetic;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   816
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   817
        Set<Requires> rqs = new HashSet<>(requires.values());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   818
        assert (rqs.stream().map(Requires::name).sorted().distinct().count()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   819
                == rqs.size())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   820
            : "Module " + name + " has duplicate requires";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   821
        this.requires = emptyOrUnmodifiableSet(rqs);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   822
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   823
        Set<Exports> exs = new HashSet<>(exports.values());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   824
        assert (exs.stream().map(Exports::source).sorted().distinct().count()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   825
                == exs.size())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   826
            : "Module " + name + " has duplicate exports";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   827
        this.exports = emptyOrUnmodifiableSet(exs);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   828
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   829
        this.uses = emptyOrUnmodifiableSet(uses);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   830
        this.provides = emptyOrUnmodifiableMap(provides);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   831
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   832
        this.version = version;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   833
        this.mainClass = mainClass;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   834
        this.osName = osName;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   835
        this.osArch = osArch;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   836
        this.osVersion = osVersion;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   837
        this.hashes = hashes;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   838
38888
dd584fbea6a2 8159334: ModuleDescriptor retains overlapping sets for all and concealed packages
redestad
parents: 37880
diff changeset
   839
        this.packages = emptyOrUnmodifiableSet(packages);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   840
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   841
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   842
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   843
     * Clones the given module descriptor with an augmented set of packages
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   844
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   845
    ModuleDescriptor(ModuleDescriptor md, Set<String> pkgs) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   846
        this.name = md.name;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   847
        this.automatic = md.automatic;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   848
        this.synthetic = md.synthetic;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   849
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   850
        this.requires = md.requires;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   851
        this.exports = md.exports;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   852
        this.uses = md.uses;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   853
        this.provides = md.provides;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   854
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   855
        this.version = md.version;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   856
        this.mainClass = md.mainClass;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   857
        this.osName = md.osName;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   858
        this.osArch = md.osArch;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   859
        this.osVersion = md.osVersion;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   860
        this.hashes = null; // need to ignore
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   861
38888
dd584fbea6a2 8159334: ModuleDescriptor retains overlapping sets for all and concealed packages
redestad
parents: 37880
diff changeset
   862
        Set<String> packages = new HashSet<>(md.packages);
dd584fbea6a2 8159334: ModuleDescriptor retains overlapping sets for all and concealed packages
redestad
parents: 37880
diff changeset
   863
        packages.addAll(pkgs);
dd584fbea6a2 8159334: ModuleDescriptor retains overlapping sets for all and concealed packages
redestad
parents: 37880
diff changeset
   864
        this.packages = emptyOrUnmodifiableSet(packages);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   865
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   866
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   867
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   868
     * Creates a module descriptor from its components. This method is intended
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   869
     * for use by the jlink plugin.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   870
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   871
    ModuleDescriptor(String name,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   872
                     boolean automatic,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   873
                     boolean synthetic,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   874
                     Set<Requires> requires,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   875
                     Set<String> uses,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   876
                     Set<Exports> exports,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   877
                     Map<String, Provides> provides,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   878
                     Version version,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   879
                     String mainClass,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   880
                     String osName,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   881
                     String osArch,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   882
                     String osVersion,
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   883
                     Set<String> packages,
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   884
                     ModuleHashes hashes) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   885
        this.name = name;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   886
        this.automatic = automatic;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   887
        this.synthetic = synthetic;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   888
        this.requires = Collections.unmodifiableSet(requires);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   889
        this.exports = Collections.unmodifiableSet(exports);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   890
        this.uses = Collections.unmodifiableSet(uses);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   891
        this.provides = Collections.unmodifiableMap(provides);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   892
        this.packages = Collections.unmodifiableSet(packages);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   893
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   894
        this.version = version;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   895
        this.mainClass = mainClass;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   896
        this.osName = osName;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   897
        this.osArch = osArch;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   898
        this.osVersion = osVersion;
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   899
        this.hashes = hashes;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   900
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   901
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   902
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   903
     * <p> The module name. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   904
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   905
     * @return The module name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   906
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   907
    public String name() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   908
        return name;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   909
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   910
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   911
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   912
     * <p> Returns {@code true} if this is an automatic module. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   913
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   914
     * <p> An automatic module is defined implicitly rather than explicitly
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   915
     * and therefore does not have a module declaration. JAR files located on
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   916
     * the application module path, or by the {@link ModuleFinder} returned by
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   917
     * {@link ModuleFinder#of(java.nio.file.Path[]) ModuleFinder.of}, are
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   918
     * treated as automatic modules if they do have not have a module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   919
     * declaration. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   920
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   921
     * @return  {@code true} if this is an automatic module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   922
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   923
    public boolean isAutomatic() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   924
        return automatic;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   925
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   926
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   927
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   928
     * <p> Returns {@code true} if this module descriptor was not generated
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   929
     * from an explicit module declaration ({@code module-info.java})
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   930
     * or an implicit module declaration (an {@link #isAutomatic() automatic}
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   931
     * module). </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   932
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   933
     * @return  {@code true} if this module descriptor was not generated by
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   934
     *          an explicit or implicit module declaration
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   935
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   936
     * @jvms 4.7.8 The {@code Synthetic} Attribute
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   937
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   938
    public boolean isSynthetic() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   939
        return synthetic;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   940
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   941
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   942
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   943
     * <p> The dependences of this module. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   944
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   945
     * @return  A possibly-empty unmodifiable set of {@link Requires} objects
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   946
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   947
    public Set<Requires> requires() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   948
        return requires;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   949
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   950
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   951
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   952
     * <p> The service dependences of this module. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   953
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   954
     * @return  A possibly-empty unmodifiable set of the fully qualified class
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   955
     *          names of the service types used
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   956
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   957
    public Set<String> uses() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   958
        return uses;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   959
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   960
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   961
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   962
     * <p> The services that this module provides. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   963
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   964
     * @return The possibly-empty unmodifiable map of the services that this
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   965
     *         module provides. The map key is fully qualified class name of
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   966
     *         the service type.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   967
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   968
    public Map<String, Provides> provides() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   969
        return provides;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   970
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   971
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   972
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   973
     * <p> The module exports. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   974
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   975
     * @return  A possibly-empty unmodifiable set of exported packages
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   976
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   977
    public Set<Exports> exports() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   978
        return exports;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   979
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   980
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   981
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   982
     * Returns this module's version.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   983
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   984
     * @return This module's version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   985
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   986
    public Optional<Version> version() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   987
        return Optional.ofNullable(version);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   988
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   989
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   990
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   991
     * Returns a string containing this module's name and, if present, its
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   992
     * version.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   993
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   994
     * @return A string containing this module's name and, if present, its
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   995
     *         version.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   996
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   997
    public String toNameAndVersion() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   998
        if (version != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   999
            return name() + "@" + version;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1000
        } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1001
            return name();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1002
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1003
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1004
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1005
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1006
     * Returns the module's main class.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1007
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1008
     * @return The fully qualified class name of this module's main class
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1009
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1010
    public Optional<String> mainClass() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1011
        return Optional.ofNullable(mainClass);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1012
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1013
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1014
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1015
     * Returns the operating system name if this module is operating system
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1016
     * specific.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1017
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1018
     * @return The operating system name or an empty {@code Optional}
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1019
     *         if this module is not operating system specific
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1020
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1021
    public Optional<String> osName() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1022
        return Optional.ofNullable(osName);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1023
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1024
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1025
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1026
     * Returns the operating system architecture if this module is operating
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1027
     * system architecture specific.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1028
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1029
     * @return The operating system architecture or an empty {@code Optional}
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1030
     *         if this module is not operating system architecture specific
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1031
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1032
    public Optional<String> osArch() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1033
        return Optional.ofNullable(osArch);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1034
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1035
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1036
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1037
     * Returns the operating system version if this module is operating
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1038
     * system version specific.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1039
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1040
     * @return The operating system version or an empty {@code Optional}
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1041
     *         if this module is not operating system version specific
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1042
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1043
    public Optional<String> osVersion() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1044
        return Optional.ofNullable(osVersion);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1045
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1046
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1047
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1048
     * Returns the names of the packages defined in, but not exported by, this
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1049
     * module.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1050
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1051
     * @return A possibly-empty unmodifiable set of the concealed packages
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1052
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1053
    public Set<String> conceals() {
38888
dd584fbea6a2 8159334: ModuleDescriptor retains overlapping sets for all and concealed packages
redestad
parents: 37880
diff changeset
  1054
        Set<String> conceals = new HashSet<>(packages);
dd584fbea6a2 8159334: ModuleDescriptor retains overlapping sets for all and concealed packages
redestad
parents: 37880
diff changeset
  1055
        exports.stream().map(Exports::source).forEach(conceals::remove);
dd584fbea6a2 8159334: ModuleDescriptor retains overlapping sets for all and concealed packages
redestad
parents: 37880
diff changeset
  1056
        return emptyOrUnmodifiableSet(conceals);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1057
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1058
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1059
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1060
     * Returns the names of all the packages defined in this module, whether
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1061
     * exported or concealed.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1062
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1063
     * @return A possibly-empty unmodifiable set of the all packages
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1064
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1065
    public Set<String> packages() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1066
        return packages;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1067
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1068
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1069
    /**
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  1070
     * Returns the object with the hashes of other modules
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1071
     */
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  1072
    Optional<ModuleHashes> hashes() {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1073
        return Optional.ofNullable(hashes);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1074
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1075
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1076
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1077
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1078
     * A builder used for building {@link ModuleDescriptor} objects.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1079
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1080
     * <p> Example usage: </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1081
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1082
     * <pre>{@code
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1083
     *     ModuleDescriptor descriptor = new ModuleDescriptor.Builder("m1")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1084
     *         .requires("m2")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1085
     *         .exports("p")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1086
     *         .build();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1087
     * }</pre>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1088
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1089
     * @apiNote A {@code Builder} cannot be used to create an {@link
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1090
     * ModuleDescriptor#isAutomatic() automatic} or a {@link
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1091
     * ModuleDescriptor#isSynthetic() synthetic} module.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1092
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1093
     * @since 9
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1094
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1095
    public static final class Builder {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1096
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1097
        final String name;
39050
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 38888
diff changeset
  1098
        boolean automatic;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1099
        boolean synthetic;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1100
        final Map<String, Requires> requires = new HashMap<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1101
        final Set<String> uses = new HashSet<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1102
        final Map<String, Exports> exports = new HashMap<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1103
        final Map<String, Provides> provides = new HashMap<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1104
        Set<String> conceals = Collections.emptySet();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1105
        Version version;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1106
        String osName;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1107
        String osArch;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1108
        String osVersion;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1109
        String mainClass;
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  1110
        ModuleHashes hashes;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1111
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1112
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1113
         * Initializes a new builder with the given module name.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1114
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1115
         * @param  name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1116
         *         The module name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1117
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1118
         * @throws IllegalArgumentException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1119
         *         If the module name is {@code null} or is not a legal Java
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1120
         *         identifier
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1121
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1122
        public Builder(String name) {
39050
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 38888
diff changeset
  1123
            this.name = requireModuleName(name);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1124
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1125
39050
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 38888
diff changeset
  1126
        /**
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 38888
diff changeset
  1127
         * Updates the builder so that it builds an automatic module.
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 38888
diff changeset
  1128
         *
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 38888
diff changeset
  1129
         * @return This builder
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 38888
diff changeset
  1130
         *
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 38888
diff changeset
  1131
         * @see ModuleDescriptor#isAutomatic()
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 38888
diff changeset
  1132
         */
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 38888
diff changeset
  1133
        /* package */ Builder automatic() {
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 38888
diff changeset
  1134
            this.automatic = true;
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 38888
diff changeset
  1135
            return this;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1136
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1137
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1138
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1139
         * Adds a dependence on a module.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1140
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1141
         * @param  req
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1142
         *         The dependence
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1143
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1144
         * @return This builder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1145
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1146
         * @throws IllegalArgumentException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1147
         *         If the dependence is on the module that this builder was
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1148
         *         initialized to build
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1149
         * @throws IllegalStateException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1150
         *         If the dependence on the module has already been declared
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1151
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1152
        public Builder requires(Requires req) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1153
            String mn = req.name();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1154
            if (name.equals(mn))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1155
                throw new IllegalArgumentException("Dependence on self");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1156
            if (requires.containsKey(mn))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1157
                throw new IllegalStateException("Dependence upon " + mn
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1158
                                                + " already declared");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1159
            requires.put(mn, req);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1160
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1161
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1162
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1163
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1164
         * Adds a dependence on a module with the given (and possibly empty)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1165
         * set of modifiers.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1166
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1167
         * @param  mods
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1168
         *         The set of modifiers
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1169
         * @param  mn
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1170
         *         The module name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1171
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1172
         * @return This builder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1173
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1174
         * @throws IllegalArgumentException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1175
         *         If the module name is {@code null}, is not a legal Java
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1176
         *         identifier, or is equal to the module name that this builder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1177
         *         was initialized to build
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1178
         * @throws IllegalStateException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1179
         *         If the dependence on the module has already been declared
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1180
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1181
        public Builder requires(Set<Requires.Modifier> mods, String mn) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1182
            if (name.equals(mn))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1183
                throw new IllegalArgumentException("Dependence on self");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1184
            if (requires.containsKey(mn))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1185
                throw new IllegalStateException("Dependence upon " + mn
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1186
                                                + " already declared");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1187
            requires.put(mn, new Requires(mods, mn)); // checks mn
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1188
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1189
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1190
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1191
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1192
         * Adds a dependence on a module with an empty set of modifiers.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1193
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1194
         * @param  mn
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1195
         *         The module name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1196
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1197
         * @return This builder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1198
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1199
         * @throws IllegalArgumentException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1200
         *         If the module name is {@code null}, is not a legal Java
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1201
         *         identifier, or is equal to the module name that this builder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1202
         *         was initialized to build
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1203
         * @throws IllegalStateException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1204
         *         If the dependence on the module has already been declared
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1205
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1206
        public Builder requires(String mn) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1207
            return requires(EnumSet.noneOf(Requires.Modifier.class), mn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1208
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1209
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1210
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1211
         * Adds a dependence on a module with the given modifier.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1212
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1213
         * @param  mod
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1214
         *         The modifier
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1215
         * @param  mn
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1216
         *         The module name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1217
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1218
         * @return This builder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1219
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1220
         * @throws IllegalArgumentException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1221
         *         If the module name is {@code null}, is not a legal Java
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1222
         *         identifier, or is equal to the module name that this builder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1223
         *         was initialized to build
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1224
         * @throws IllegalStateException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1225
         *         If the dependence on the module has already been declared
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1226
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1227
        public Builder requires(Requires.Modifier mod, String mn) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1228
            return requires(EnumSet.of(mod), mn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1229
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1230
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1231
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1232
         * Adds a service dependence.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1233
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1234
         * @param  st
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1235
         *         The service type
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1236
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1237
         * @return This builder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1238
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1239
         * @throws IllegalArgumentException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1240
         *         If the service type is {@code null} or is not a legal Java
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1241
         *         identifier
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1242
         * @throws IllegalStateException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1243
         *         If a dependency on the service type has already been declared
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1244
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1245
        public Builder uses(String st) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1246
            if (uses.contains(requireServiceTypeName(st)))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1247
                throw new IllegalStateException("Dependence upon service "
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1248
                                                + st + " already declared");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1249
            uses.add(st);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1250
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1251
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1252
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1253
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1254
         * Ensures that the given package name has not been declared as an
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1255
         * exported or concealed package.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1256
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1257
        private void ensureNotExportedOrConcealed(String pn) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1258
            if (exports.containsKey(pn))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1259
                throw new IllegalStateException("Export of package "
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1260
                                                + pn + " already declared");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1261
            if (conceals.contains(pn))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1262
                throw new IllegalStateException("Concealed package "
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1263
                                                + pn + " already declared");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1264
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1265
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1266
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1267
         * Adds an export.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1268
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1269
         * @param  e
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1270
         *         The export
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1271
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1272
         * @return This builder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1273
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1274
         * @throws IllegalStateException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1275
         *         If the package is already declared as an exported or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1276
         *         concealed package
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1277
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1278
        public Builder exports(Exports e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1279
            String pn = e.source();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1280
            ensureNotExportedOrConcealed(pn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1281
            exports.put(pn, e);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1282
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1283
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1284
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1285
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1286
         * Adds an export to a set of target modules.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1287
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1288
         * @param  pn
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1289
         *         The package name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1290
         * @param  targets
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1291
         *         The set of target modules names
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1292
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1293
         * @return This builder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1294
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1295
         * @throws IllegalArgumentException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1296
         *         If the package name or any of the target modules is {@code
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1297
         *         null} or is not a legal Java identifier, or the set of
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1298
         *         targets is empty
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1299
         * @throws IllegalStateException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1300
         *         If the package is already declared as an exported or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1301
         *         concealed package
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1302
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1303
        public Builder exports(String pn, Set<String> targets) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1304
            ensureNotExportedOrConcealed(pn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1305
            exports.put(pn, new Exports(pn, targets)); // checks pn and targets
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1306
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1307
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1308
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1309
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1310
         * Adds an export to a target module.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1311
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1312
         * @param  pn
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1313
         *         The package name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1314
         * @param  target
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1315
         *         The target module name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1316
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1317
         * @return This builder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1318
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1319
         * @throws IllegalArgumentException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1320
         *         If the package name or target module is {@code null} or is
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1321
         *         not a legal Java identifier
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1322
         * @throws IllegalStateException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1323
         *         If the package is already declared as an exported or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1324
         *         concealed package
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1325
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1326
        public Builder exports(String pn, String target) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1327
            return exports(pn, Collections.singleton(target));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1328
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1329
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1330
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1331
         * Adds an export.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1332
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1333
         * @param  pn
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1334
         *         The package name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1335
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1336
         * @return This builder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1337
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1338
         * @throws IllegalArgumentException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1339
         *         If the package name is {@code null} or is not a legal Java
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1340
         *         identifier
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1341
         * @throws IllegalStateException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1342
         *         If the package is already declared as an exported or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1343
         *         concealed package
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1344
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1345
        public Builder exports(String pn) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1346
            ensureNotExportedOrConcealed(pn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1347
            exports.put(pn, new Exports(pn)); // checks pn
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1348
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1349
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1350
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1351
        // Used by ModuleInfo, after a packageFinder is invoked
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1352
        /* package */ Set<String> exportedPackages() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1353
            return exports.keySet();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1354
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1355
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1356
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1357
         * Provides a service with one or more implementations.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1358
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1359
         * @param  p
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1360
         *         The provides
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1361
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1362
         * @return This builder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1363
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1364
         * @throws IllegalStateException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1365
         *         If the providers for the service type have already been
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1366
         *         declared
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1367
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1368
        public Builder provides(Provides p) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1369
            String st = p.service();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1370
            if (provides.containsKey(st))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1371
                throw new IllegalStateException("Providers of service "
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1372
                                                + st + " already declared");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1373
            provides.put(st, p);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1374
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1375
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1376
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1377
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1378
         * Provides service {@code st} with implementations {@code pcs}.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1379
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1380
         * @param  st
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1381
         *         The service type
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1382
         * @param  pcs
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1383
         *         The set of provider class names
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1384
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1385
         * @return This builder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1386
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1387
         * @throws IllegalArgumentException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1388
         *         If the service type or any of the provider class names is
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1389
         *         {@code null} or is not a legal Java identifier, or the set
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1390
         *         of provider class names is empty
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1391
         * @throws IllegalStateException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1392
         *         If the providers for the service type have already been
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1393
         *         declared
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1394
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1395
        public Builder provides(String st, Set<String> pcs) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1396
            if (provides.containsKey(st))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1397
                throw new IllegalStateException("Providers of service "
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1398
                                                + st + " already declared");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1399
            provides.put(st, new Provides(st, pcs)); // checks st and pcs
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1400
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1401
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1402
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1403
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1404
         * Provides service {@code st} with implementation {@code pc}.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1405
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1406
         * @param  st
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1407
         *         The service type
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1408
         * @param  pc
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1409
         *         The provider class name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1410
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1411
         * @return This builder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1412
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1413
         * @throws IllegalArgumentException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1414
         *         If the service type or the provider class name is {@code
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1415
         *         null} or is not a legal Java identifier
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1416
         * @throws IllegalStateException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1417
         *         If the providers for the service type have already been
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1418
         *         declared
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1419
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1420
        public Builder provides(String st, String pc) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1421
            return provides(st, Collections.singleton(pc));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1422
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1423
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1424
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1425
         * Adds a set of (possible empty) concealed packages.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1426
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1427
         * @param  pns
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1428
         *         The set of package names of the concealed packages
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1429
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1430
         * @return This builder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1431
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1432
         * @throws IllegalArgumentException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1433
         *         If any of the package names is {@code null} or is not a
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1434
         *         legal Java identifier
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1435
         * @throws IllegalStateException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1436
         *         If any of packages are already declared as a concealed or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1437
         *         exported package
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1438
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1439
        public Builder conceals(Set<String> pns) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1440
            pns.forEach(this::conceals);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1441
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1442
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1443
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1444
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1445
         * Adds a concealed package.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1446
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1447
         * @param  pn
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1448
         *         The package name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1449
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1450
         * @return This builder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1451
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1452
         * @throws IllegalArgumentException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1453
         *         If the package name is {@code null}, or is not a legal Java
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1454
         *         identifier
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1455
         * @throws IllegalStateException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1456
         *         If the package is already declared as a concealed or exported
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1457
         *         package
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1458
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1459
        public Builder conceals(String pn) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1460
            Checks.requirePackageName(pn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1461
            ensureNotExportedOrConcealed(pn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1462
            if (conceals.isEmpty())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1463
                conceals = new HashSet<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1464
            conceals.add(pn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1465
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1466
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1467
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1468
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1469
         * Sets the module version.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1470
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1471
         * @param  v
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1472
         *         The version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1473
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1474
         * @return This builder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1475
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1476
         * @throws IllegalStateException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1477
         *         If the module version is already set
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1478
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1479
        public Builder version(Version v) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1480
            if (version != null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1481
                throw new IllegalStateException("module version already set");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1482
            version = requireNonNull(v);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1483
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1484
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1485
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1486
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1487
         * Sets the module version.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1488
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1489
         * @param  v
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1490
         *         The version string to parse
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1491
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1492
         * @return This builder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1493
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1494
         * @throws IllegalArgumentException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1495
         *         If {@code v} is null or cannot be parsed as a version string
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1496
         * @throws IllegalStateException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1497
         *         If the module version is already set
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1498
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1499
         * @see Version#parse(String)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1500
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1501
        public Builder version(String v) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1502
            if (version != null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1503
                throw new IllegalStateException("module version already set");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1504
            version = Version.parse(v);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1505
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1506
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1507
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1508
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1509
         * Sets the module main class.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1510
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1511
         * @param  mc
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1512
         *         The module main class
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1513
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1514
         * @return This builder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1515
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1516
         * @throws IllegalArgumentException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1517
         *         If {@code mainClass} is null or is not a legal Java identifier
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1518
         * @throws IllegalStateException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1519
         *         If the module main class is already set
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1520
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1521
        public Builder mainClass(String mc) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1522
            if (mainClass != null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1523
                throw new IllegalStateException("main class already set");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1524
            mainClass = requireJavaIdentifier("main class name", mc);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1525
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1526
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1527
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1528
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1529
         * Sets the operating system name.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1530
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1531
         * @param  name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1532
         *         The operating system name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1533
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1534
         * @return This builder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1535
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1536
         * @throws IllegalArgumentException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1537
         *         If {@code name} is null or the empty String
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1538
         * @throws IllegalStateException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1539
         *         If the operating system name is already set
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1540
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1541
        public Builder osName(String name) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1542
            if (osName != null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1543
                throw new IllegalStateException("OS name already set");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1544
            if (name == null || name.isEmpty())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1545
                throw new IllegalArgumentException("OS name is null or empty");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1546
            osName = name;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1547
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1548
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1549
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1550
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1551
         * Sets the operating system architecture.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1552
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1553
         * @param  arch
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1554
         *         The operating system architecture
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1555
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1556
         * @return This builder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1557
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1558
         * @throws IllegalArgumentException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1559
         *         If {@code name} is null or the empty String
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1560
         * @throws IllegalStateException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1561
         *         If the operating system architecture is already set
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1562
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1563
        public Builder osArch(String arch) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1564
            if (osArch != null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1565
                throw new IllegalStateException("OS arch already set");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1566
            if (arch == null || arch.isEmpty())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1567
                throw new IllegalArgumentException("OS arch is null or empty");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1568
            osArch = arch;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1569
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1570
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1571
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1572
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1573
         * Sets the operating system version.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1574
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1575
         * @param  version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1576
         *         The operating system version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1577
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1578
         * @return This builder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1579
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1580
         * @throws IllegalArgumentException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1581
         *         If {@code name} is null or the empty String
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1582
         * @throws IllegalStateException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1583
         *         If the operating system version is already set
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1584
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1585
        public Builder osVersion(String version) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1586
            if (osVersion != null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1587
                throw new IllegalStateException("OS version already set");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1588
            if (version == null || version.isEmpty())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1589
                throw new IllegalArgumentException("OS version is null or empty");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1590
            osVersion = version;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1591
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1592
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1593
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  1594
        /* package */ Builder hashes(ModuleHashes hashes) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1595
            this.hashes = hashes;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1596
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1597
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1598
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1599
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1600
        /* package */ Builder synthetic(boolean v) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1601
            this.synthetic = v;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1602
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1603
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1604
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1605
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1606
         * Builds and returns a {@code ModuleDescriptor} from its components.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1607
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1608
         * @return The module descriptor
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1609
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1610
        public ModuleDescriptor build() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1611
            assert name != null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1612
38888
dd584fbea6a2 8159334: ModuleDescriptor retains overlapping sets for all and concealed packages
redestad
parents: 37880
diff changeset
  1613
            Set<String> packages = new HashSet<>(conceals);
dd584fbea6a2 8159334: ModuleDescriptor retains overlapping sets for all and concealed packages
redestad
parents: 37880
diff changeset
  1614
            packages.addAll(exportedPackages());
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1615
            return new ModuleDescriptor(name,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1616
                                        automatic,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1617
                                        synthetic,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1618
                                        requires,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1619
                                        uses,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1620
                                        exports,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1621
                                        provides,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1622
                                        version,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1623
                                        mainClass,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1624
                                        osName,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1625
                                        osArch,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1626
                                        osVersion,
38888
dd584fbea6a2 8159334: ModuleDescriptor retains overlapping sets for all and concealed packages
redestad
parents: 37880
diff changeset
  1627
                                        packages,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1628
                                        hashes);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1629
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1630
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1631
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1632
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1633
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1634
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1635
     * Compares this module descriptor to another.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1636
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1637
     * <p> Two {@code ModuleDescriptor} objects are compared by comparing their
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1638
     * module name lexicographically.  Where the module names are equal then
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1639
     * the versions, if present, are compared. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1640
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1641
     * @apiNote For now, the natural ordering is not consistent with equals.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1642
     * If two module descriptors have equal module names, equal versions if
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1643
     * present, but their corresponding components are not equal, then they
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1644
     * will be considered equal by this method.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1645
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1646
     * @param  that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1647
     *         The object to which this module descriptor is to be compared
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1648
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1649
     * @return A negative integer, zero, or a positive integer if this module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1650
     *         descriptor is less than, equal to, or greater than the given
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1651
     *         module descriptor
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1652
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1653
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1654
    public int compareTo(ModuleDescriptor that) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1655
        int c = this.name().compareTo(that.name());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1656
        if (c != 0) return c;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1657
        if (version == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1658
            if (that.version == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1659
                return 0;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1660
            return -1;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1661
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1662
        if (that.version == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1663
            return +1;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1664
        return version.compareTo(that.version);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1665
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1666
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1667
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1668
     * Tests this module descriptor for equality with the given object.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1669
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1670
     * <p> If the given object is not a {@code ModuleDescriptor} then this
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1671
     * method returns {@code false}. Two module descriptors are equal if each
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1672
     * of their corresponding components is equal. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1673
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1674
     * <p> This method satisfies the general contract of the {@link
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1675
     * java.lang.Object#equals(Object) Object.equals} method. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1676
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1677
     * @param   ob
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1678
     *          the object to which this object is to be compared
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1679
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1680
     * @return  {@code true} if, and only if, the given object is a module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1681
     *          descriptor that is equal to this module descriptor
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1682
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1683
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1684
    public boolean equals(Object ob) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1685
        if (ob == this)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1686
            return true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1687
        if (!(ob instanceof ModuleDescriptor))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1688
            return false;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1689
        ModuleDescriptor that = (ModuleDescriptor)ob;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1690
        return (name.equals(that.name)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1691
                && automatic == that.automatic
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1692
                && synthetic == that.synthetic
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1693
                && requires.equals(that.requires)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1694
                && uses.equals(that.uses)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1695
                && exports.equals(that.exports)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1696
                && provides.equals(that.provides)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1697
                && Objects.equals(version, that.version)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1698
                && Objects.equals(mainClass, that.mainClass)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1699
                && Objects.equals(osName, that.osName)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1700
                && Objects.equals(osArch, that.osArch)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1701
                && Objects.equals(osVersion, that.osVersion)
38888
dd584fbea6a2 8159334: ModuleDescriptor retains overlapping sets for all and concealed packages
redestad
parents: 37880
diff changeset
  1702
                && Objects.equals(packages, that.packages)
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1703
                && Objects.equals(hashes, that.hashes));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1704
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1705
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1706
    private transient int hash;  // cached hash code
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1707
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1708
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1709
     * Computes a hash code for this module descriptor.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1710
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1711
     * <p> The hash code is based upon the components of the module descriptor,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1712
     * and satisfies the general contract of the {@link Object#hashCode
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1713
     * Object.hashCode} method. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1714
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1715
     * @return The hash-code value for this module descriptor
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1716
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1717
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1718
    public int hashCode() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1719
        int hc = hash;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1720
        if (hc == 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1721
            hc = name.hashCode();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1722
            hc = hc * 43 + Boolean.hashCode(automatic);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1723
            hc = hc * 43 + Boolean.hashCode(synthetic);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1724
            hc = hc * 43 + requires.hashCode();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1725
            hc = hc * 43 + uses.hashCode();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1726
            hc = hc * 43 + exports.hashCode();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1727
            hc = hc * 43 + provides.hashCode();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1728
            hc = hc * 43 + Objects.hashCode(version);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1729
            hc = hc * 43 + Objects.hashCode(mainClass);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1730
            hc = hc * 43 + Objects.hashCode(osName);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1731
            hc = hc * 43 + Objects.hashCode(osArch);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1732
            hc = hc * 43 + Objects.hashCode(osVersion);
38888
dd584fbea6a2 8159334: ModuleDescriptor retains overlapping sets for all and concealed packages
redestad
parents: 37880
diff changeset
  1733
            hc = hc * 43 + Objects.hashCode(packages);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1734
            hc = hc * 43 + Objects.hashCode(hashes);
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  1735
            if (hc == 0)
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  1736
                hc = -1;
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  1737
            hash = hc;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1738
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1739
        return hc;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1740
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1741
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1742
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1743
     * Returns a string describing this descriptor.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1744
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1745
     * @return A string describing this descriptor
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1746
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1747
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1748
    public String toString() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1749
        StringBuilder sb = new StringBuilder();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1750
        sb.append("Module { name: ").append(toNameAndVersion());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1751
        if (!requires.isEmpty())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1752
            sb.append(", ").append(requires);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1753
        if (!uses.isEmpty())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1754
            sb.append(", ").append(uses);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1755
        if (!exports.isEmpty())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1756
            sb.append(", exports: ").append(exports);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1757
        if (!provides.isEmpty()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1758
            sb.append(", provides: [");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1759
            for (Map.Entry<String, Provides> entry : provides.entrySet()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1760
                sb.append(entry.getKey())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1761
                   .append(" with ")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1762
                   .append(entry.getValue());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1763
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1764
            sb.append("]");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1765
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1766
        sb.append(" }");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1767
        return sb.toString();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1768
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1769
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1770
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1771
     * Reads the binary form of a module declaration from an input stream
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1772
     * as a module descriptor.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1773
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1774
     * <p> If the descriptor encoded in the input stream does not indicate a
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1775
     * set of concealed packages then the {@code packageFinder} will be
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1776
     * invoked.  The packages it returns, except for those indicated as
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1777
     * exported in the encoded descriptor, will be considered to be concealed.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1778
     * If the {@code packageFinder} throws an {@link UncheckedIOException} then
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1779
     * {@link IOException} cause will be re-thrown. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1780
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1781
     * <p> If there are bytes following the module descriptor then it is
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1782
     * implementation specific as to whether those bytes are read, ignored,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1783
     * or reported as an {@code InvalidModuleDescriptorException}. If this
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1784
     * method fails with an {@code InvalidModuleDescriptorException} or {@code
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1785
     * IOException} then it may do so after some, but not all, bytes have
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1786
     * been read from the input stream. It is strongly recommended that the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1787
     * stream be promptly closed and discarded if an exception occurs. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1788
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1789
     * @apiNote The {@code packageFinder} parameter is for use when reading
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1790
     * module descriptors from legacy module-artifact formats that do not
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1791
     * record the set of concealed packages in the descriptor itself.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1792
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1793
     * @param  in
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1794
     *         The input stream
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1795
     * @param  packageFinder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1796
     *         A supplier that can produce a set of package names
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1797
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1798
     * @return The module descriptor
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1799
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1800
     * @throws InvalidModuleDescriptorException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1801
     *         If an invalid module descriptor is detected
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1802
     * @throws IOException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1803
     *         If an I/O error occurs reading from the input stream or {@code
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1804
     *         UncheckedIOException} is thrown by the package finder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1805
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1806
    public static ModuleDescriptor read(InputStream in,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1807
                                        Supplier<Set<String>> packageFinder)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1808
        throws IOException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1809
    {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1810
        return ModuleInfo.read(in, requireNonNull(packageFinder));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1811
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1812
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1813
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1814
     * Reads the binary form of a module declaration from an input stream
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1815
     * as a module descriptor.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1816
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1817
     * @param  in
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1818
     *         The input stream
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1819
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1820
     * @return The module descriptor
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1821
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1822
     * @throws InvalidModuleDescriptorException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1823
     *         If an invalid module descriptor is detected
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1824
     * @throws IOException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1825
     *         If an I/O error occurs reading from the input stream
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1826
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1827
    public static ModuleDescriptor read(InputStream in) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1828
        return ModuleInfo.read(in, null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1829
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1830
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1831
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1832
     * Reads the binary form of a module declaration from a byte buffer
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1833
     * as a module descriptor.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1834
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1835
     * <p> If the descriptor encoded in the byte buffer does not indicate a
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1836
     * set of concealed packages then the {@code packageFinder} will be
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1837
     * invoked.  The packages it returns, except for those indicated as
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1838
     * exported in the encoded descriptor, will be considered to be
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1839
     * concealed. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1840
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1841
     * <p> The module descriptor is read from the buffer stating at index
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1842
     * {@code p}, where {@code p} is the buffer's {@link ByteBuffer#position()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1843
     * position} when this method is invoked. Upon return the buffer's position
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1844
     * will be equal to {@code p + n} where {@code n} is the number of bytes
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1845
     * read from the buffer. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1846
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1847
     * <p> If there are bytes following the module descriptor then it is
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1848
     * implementation specific as to whether those bytes are read, ignored,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1849
     * or reported as an {@code InvalidModuleDescriptorException}. If this
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1850
     * method fails with an {@code InvalidModuleDescriptorException} then it
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1851
     * may do so after some, but not all, bytes have been read. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1852
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1853
     * @apiNote The {@code packageFinder} parameter is for use when reading
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1854
     * module descriptors from legacy module-artifact formats that do not
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1855
     * record the set of concealed packages in the descriptor itself.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1856
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1857
     * @param  bb
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1858
     *         The byte buffer
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1859
     * @param  packageFinder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1860
     *         A supplier that can produce a set of package names
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1861
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1862
     * @return The module descriptor
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1863
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1864
     * @throws InvalidModuleDescriptorException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1865
     *         If an invalid module descriptor is detected
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1866
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1867
    public static ModuleDescriptor read(ByteBuffer bb,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1868
                                        Supplier<Set<String>> packageFinder)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1869
    {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1870
        return ModuleInfo.read(bb, requireNonNull(packageFinder));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1871
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1872
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1873
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1874
     * Reads the binary form of a module declaration from a byte buffer
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1875
     * as a module descriptor.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1876
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1877
     * @param  bb
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1878
     *         The byte buffer
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1879
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1880
     * @return The module descriptor
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1881
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1882
     * @throws InvalidModuleDescriptorException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1883
     *         If an invalid module descriptor is detected
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1884
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1885
    public static ModuleDescriptor read(ByteBuffer bb) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1886
        return ModuleInfo.read(bb, null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1887
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1888
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1889
    private static <K,V> Map<K,V> emptyOrUnmodifiableMap(Map<K,V> map) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1890
        if (map.isEmpty()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1891
            return Collections.emptyMap();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1892
        } else if (map.size() == 1) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1893
            Map.Entry<K, V> entry = map.entrySet().iterator().next();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1894
            return Collections.singletonMap(entry.getKey(), entry.getValue());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1895
        } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1896
            return Collections.unmodifiableMap(map);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1897
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1898
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1899
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1900
    private static <T> Set<T> emptyOrUnmodifiableSet(Set<T> set) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1901
        if (set.isEmpty()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1902
            return Collections.emptySet();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1903
        } else if (set.size() == 1) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1904
            return Collections.singleton(set.iterator().next());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1905
        } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1906
            return Collections.unmodifiableSet(set);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1907
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1908
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1909
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1910
    static {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1911
        /**
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  1912
         * Setup the shared secret to allow code in other packages access
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  1913
         * private package methods in java.lang.module.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1914
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1915
        jdk.internal.misc.SharedSecrets
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1916
            .setJavaLangModuleAccess(new jdk.internal.misc.JavaLangModuleAccess() {
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  1917
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1918
                @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1919
                public Requires newRequires(Set<Requires.Modifier> ms, String mn) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1920
                    return new Requires(ms, mn, false);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1921
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1922
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1923
                @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1924
                public Exports newExports(String source, Set<String> targets) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1925
                    return new Exports(source, targets, false);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1926
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1927
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1928
                @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1929
                public Exports newExports(String source) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1930
                    return new Exports(source, false);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1931
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1932
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1933
                @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1934
                public Provides newProvides(String service, Set<String> providers) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1935
                    return new Provides(service, providers, false);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1936
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1937
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1938
                @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1939
                public Version newVersion(String v) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1940
                    return new Version(v);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1941
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1942
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1943
                @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1944
                public ModuleDescriptor newModuleDescriptor(ModuleDescriptor md,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1945
                                                            Set<String> pkgs) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1946
                    return new ModuleDescriptor(md, pkgs);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1947
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1948
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1949
                @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1950
                public ModuleDescriptor newModuleDescriptor(String name,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1951
                                                            boolean automatic,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1952
                                                            boolean synthetic,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1953
                                                            Set<Requires> requires,
38888
dd584fbea6a2 8159334: ModuleDescriptor retains overlapping sets for all and concealed packages
redestad
parents: 37880
diff changeset
  1954
                                                            Set<String> uses,
dd584fbea6a2 8159334: ModuleDescriptor retains overlapping sets for all and concealed packages
redestad
parents: 37880
diff changeset
  1955
                                                            Set<Exports> exports,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1956
                                                            Map<String, Provides> provides,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1957
                                                            Version version,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1958
                                                            String mainClass,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1959
                                                            String osName,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1960
                                                            String osArch,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1961
                                                            String osVersion,
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  1962
                                                            Set<String> packages,
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  1963
                                                            ModuleHashes hashes) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1964
                    return new ModuleDescriptor(name,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1965
                                                automatic,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1966
                                                synthetic,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1967
                                                requires,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1968
                                                uses,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1969
                                                exports,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1970
                                                provides,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1971
                                                version,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1972
                                                mainClass,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1973
                                                osName,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1974
                                                osArch,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1975
                                                osVersion,
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  1976
                                                packages,
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  1977
                                                hashes);
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  1978
                }
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  1979
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  1980
                @Override
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  1981
                public Configuration resolveRequiresAndUses(ModuleFinder finder,
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  1982
                                                            Collection<String> roots,
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  1983
                                                            boolean check,
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  1984
                                                            PrintStream traceOutput)
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  1985
                {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  1986
                    return Configuration.resolveRequiresAndUses(finder, roots, check, traceOutput);
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  1987
                }
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  1988
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  1989
                @Override
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  1990
                public ModuleReference newPatchedModule(ModuleDescriptor descriptor,
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  1991
                                                        URI location,
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  1992
                                                        Supplier<ModuleReader> s) {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  1993
                    return new ModuleReference(descriptor, location, s, true, null);
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  1994
                }
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  1995
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  1996
                @Override
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  1997
                public Optional<ModuleHashes> hashes(ModuleDescriptor descriptor) {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  1998
                    return descriptor.hashes();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1999
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2000
            });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2001
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2002
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2003
}