src/java.base/share/classes/java/lang/module/ModuleDescriptor.java
author mchung
Tue, 06 Nov 2018 10:01:16 -0800
changeset 52427 3c6aa484536c
parent 51327 a19fda433921
permissions -rw-r--r--
8211122: Reduce the number of internal classes made accessible to jdk.unsupported Reviewed-by: alanb, dfuchs, kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
52427
3c6aa484536c 8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents: 51327
diff changeset
     2
 * Copyright (c) 2009, 2018, Oracle and/or its affiliates. All rights reserved.
36511
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;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
import java.nio.ByteBuffer;
41817
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 39050
diff changeset
    33
import java.nio.file.Path;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.util.ArrayList;
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    35
import java.util.Arrays;
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    36
import java.util.Collection;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import java.util.Collections;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
import java.util.EnumSet;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
import java.util.HashMap;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
import java.util.HashSet;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
import java.util.List;
48261
43edfde828ab 8193454: ModuleDescriptor.{Requires,Exports,Open} toString should use toLowerCase(Local.ROOT)
alanb
parents: 47866
diff changeset
    42
import java.util.Locale;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
import java.util.Map;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
import java.util.Objects;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
import java.util.Optional;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
import java.util.Set;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
import java.util.function.Supplier;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
    48
import java.util.stream.Collectors;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
    49
import java.util.stream.Stream;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
import static jdk.internal.module.Checks.*;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
import static java.util.Objects.*;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
import jdk.internal.module.Checks;
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
    55
import jdk.internal.module.ModuleInfo;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
/**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
 * A module descriptor.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
 *
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    61
 * <p> A module descriptor describes a named module and defines methods to
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    62
 * obtain each of its components. The module descriptor for a named module
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    63
 * in the Java virtual machine is obtained by invoking the {@link
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44359
diff changeset
    64
 * java.lang.Module Module}'s {@link java.lang.Module#getDescriptor
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    65
 * getDescriptor} method. Module descriptors can also be created using the
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    66
 * {@link ModuleDescriptor.Builder} class or by reading the binary form of a
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    67
 * module declaration ({@code module-info.class}) using the {@link
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    68
 * #read(InputStream,Supplier) read} methods defined here. </p>
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    69
 *
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    70
 * <p> A module descriptor describes a <em>normal</em>, open, or automatic
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    71
 * module. <em>Normal</em> modules and open modules describe their {@link
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    72
 * #requires() dependences}, {@link #exports() exported-packages}, the services
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    73
 * that they {@link #uses() use} or {@link #provides() provide}, and other
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    74
 * components. <em>Normal</em> modules may {@link #opens() open} specific
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    75
 * packages. The module descriptor for an open modules does not declare any
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    76
 * open packages (its {@code opens} method returns an empty set) but when
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    77
 * instantiated in the Java virtual machine then it is treated as if all
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    78
 * packages are open. The module descriptor for an automatic module does not
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    79
 * declare any dependences (except for the mandatory dependency on {@code
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    80
 * java.base}), and does not declare any exported or open packages. Automatic
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    81
 * module receive special treatment during resolution so that they read all
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    82
 * other modules in the configuration. When an automatic module is instantiated
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    83
 * in the Java virtual machine then it reads every unnamed module and is
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    84
 * treated as if all packages are exported and open. </p>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
 * <p> {@code ModuleDescriptor} objects are immutable and safe for use by
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
 * multiple concurrent threads.</p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
 *
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44359
diff changeset
    89
 * @see java.lang.Module
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
 * @since 9
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    91
 * @spec JPMS
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
public class ModuleDescriptor
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
    implements Comparable<ModuleDescriptor>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
{
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
    /**
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    99
     * A modifier on a module.
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   100
     *
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   101
     * @see ModuleDescriptor#modifiers()
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   102
     * @since 9
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
   103
     * @spec JPMS
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   104
     */
51327
a19fda433921 8209003: Consolidate use of empty collections in java.lang.module
redestad
parents: 49529
diff changeset
   105
    public enum Modifier {
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   106
        /**
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   107
         * An open module. An open module does not declare any open packages
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   108
         * but the resulting module is treated as if all packages are open.
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   109
         */
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   110
        OPEN,
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   111
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   112
        /**
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   113
         * An automatic module. An automatic module is treated as if it exports
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   114
         * and opens all packages.
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   115
         *
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   116
         * @apiNote This modifier does not correspond to a module flag in the
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   117
         * binary form of a module declaration ({@code module-info.class}).
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   118
         */
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   119
        AUTOMATIC,
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   120
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   121
        /**
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   122
         * The module was not explicitly or implicitly declared.
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   123
         */
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   124
        SYNTHETIC,
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   125
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   126
        /**
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   127
         * The module was implicitly declared.
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   128
         */
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   129
        MANDATED;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   130
    }
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   131
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   132
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   133
    /**
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
     * <p> A dependence upon a module </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
     * @see ModuleDescriptor#requires()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
     * @since 9
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   138
     * @spec JPMS
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   139
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   140
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   141
    public final static class Requires
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   142
        implements Comparable<Requires>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   143
    {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   144
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   145
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   146
         * A modifier on a module dependence.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   147
         *
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   148
         * @see Requires#modifiers()
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   149
         * @since 9
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   150
         * @spec JPMS
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
         */
51327
a19fda433921 8209003: Consolidate use of empty collections in java.lang.module
redestad
parents: 49529
diff changeset
   152
        public enum Modifier {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   153
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   154
            /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   155
             * The dependence causes any module which depends on the <i>current
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   156
             * module</i> to have an implicitly declared dependence on the module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   157
             * named by the {@code Requires}.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   158
             */
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   159
            TRANSITIVE,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   160
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   161
            /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   162
             * The dependence is mandatory in the static phase, during compilation,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   163
             * but is optional in the dynamic phase, during execution.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   164
             */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   165
            STATIC,
36511
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
             * The dependence was not explicitly or implicitly declared in the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   169
             * source of the module declaration.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   170
             */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   171
            SYNTHETIC,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   172
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   173
            /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   174
             * The dependence was implicitly declared in the source of the module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   175
             * declaration.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   176
             */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   177
            MANDATED;
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
        private final Set<Modifier> mods;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   182
        private final String name;
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   183
        private final Version compiledVersion;
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   184
        private final String rawCompiledVersion;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   185
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   186
        private Requires(Set<Modifier> ms, String mn, Version v, String vs) {
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   187
            assert v == null || vs == null;
51327
a19fda433921 8209003: Consolidate use of empty collections in java.lang.module
redestad
parents: 49529
diff changeset
   188
            this.mods = Set.copyOf(ms);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   189
            this.name = mn;
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   190
            this.compiledVersion = v;
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   191
            this.rawCompiledVersion = vs;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   192
        }
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   193
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   194
        private Requires(Set<Modifier> ms, String mn, Version v, boolean unused) {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   195
            this.mods = ms;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   196
            this.name = mn;
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   197
            this.compiledVersion = v;
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   198
            this.rawCompiledVersion = null;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   199
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   200
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   201
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   202
         * Returns the set of modifiers.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   203
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   204
         * @return A possibly-empty unmodifiable set of modifiers
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   205
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   206
        public Set<Modifier> modifiers() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   207
            return mods;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   208
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   209
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   210
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   211
         * Return the module name.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   212
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   213
         * @return The module name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   214
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   215
        public String name() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   216
            return name;
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
        /**
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   220
         * Returns the version of the module if recorded at compile-time.
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   221
         *
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   222
         * @return The version of the module if recorded at compile-time,
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   223
         *         or an empty {@code Optional} if no version was recorded or
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   224
         *         the version string recorded is {@linkplain Version#parse(String)
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   225
         *         unparseable}
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   226
         */
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   227
        public Optional<Version> compiledVersion() {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   228
            return Optional.ofNullable(compiledVersion);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   229
        }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   230
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   231
        /**
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   232
         * Returns the string with the possibly-unparseable version of the module
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   233
         * if recorded at compile-time.
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   234
         *
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   235
         * @return The string containing the version of the module if recorded
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   236
         *         at compile-time, or an empty {@code Optional} if no version
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   237
         *         was recorded
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   238
         *
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   239
         * @see #compiledVersion()
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   240
         */
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   241
        public Optional<String> rawCompiledVersion() {
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   242
            if (compiledVersion != null) {
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   243
                return Optional.of(compiledVersion.toString());
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   244
            } else {
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   245
                return Optional.ofNullable(rawCompiledVersion);
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   246
            }
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   247
        }
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   248
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   249
        /**
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   250
         * Compares this module dependence to another.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   251
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   252
         * <p> Two {@code Requires} objects are compared by comparing their
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   253
         * module names lexicographically. Where the module names are equal
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   254
         * then the sets of modifiers are compared in the same way that
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   255
         * module modifiers are compared (see {@link ModuleDescriptor#compareTo
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   256
         * ModuleDescriptor.compareTo}). Where the module names are equal and
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   257
         * the set of modifiers are equal then the version of the modules
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   258
         * recorded at compile-time are compared. When comparing the versions
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   259
         * recorded at compile-time then a dependence that has a recorded
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   260
         * version is considered to succeed a dependence that does not have a
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   261
         * recorded version. If both recorded versions are {@linkplain
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   262
         * Version#parse(String) unparseable} then the {@linkplain
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   263
         * #rawCompiledVersion() raw version strings} are compared
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   264
         * lexicographically. </p>
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   265
         *
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   266
         * @param  that
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   267
         *         The module dependence to compare
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   268
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   269
         * @return A negative integer, zero, or a positive integer if this module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   270
         *         dependence is less than, equal to, or greater than the given
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   271
         *         module dependence
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   272
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   273
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   274
        public int compareTo(Requires that) {
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   275
            if (this == that) return 0;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   276
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   277
            int c = this.name().compareTo(that.name());
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   278
            if (c != 0) return c;
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   279
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   280
            // modifiers
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   281
            long v1 = modsValue(this.modifiers());
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   282
            long v2 = modsValue(that.modifiers());
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   283
            c = Long.compare(v1, v2);
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   284
            if (c != 0) return c;
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   285
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   286
            // compiledVersion
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   287
            c = compare(this.compiledVersion, that.compiledVersion);
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   288
            if (c != 0) return c;
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   289
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   290
            // rawCompiledVersion
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   291
            c = compare(this.rawCompiledVersion, that.rawCompiledVersion);
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   292
            if (c != 0) return c;
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   293
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   294
            return 0;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   295
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   296
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   297
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   298
         * Tests this module dependence for equality with the given object.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   299
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   300
         * <p> If the given object is not a {@code Requires} then this method
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   301
         * returns {@code false}. Two module dependence objects are equal if
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   302
         * the module names are equal, set of modifiers are equal, and the
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   303
         * compiled version of both modules is equal or not recorded for
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   304
         * both modules. </p>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   305
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   306
         * <p> This method satisfies the general contract of the {@link
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   307
         * java.lang.Object#equals(Object) Object.equals} method. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   308
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   309
         * @param   ob
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   310
         *          the object to which this object is to be compared
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   311
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   312
         * @return  {@code true} if, and only if, the given object is a module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   313
         *          dependence that is equal to this module dependence
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   314
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   315
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   316
        public boolean equals(Object ob) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   317
            if (!(ob instanceof Requires))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   318
                return false;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   319
            Requires that = (Requires)ob;
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   320
            return name.equals(that.name) && mods.equals(that.mods)
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   321
                    && Objects.equals(compiledVersion, that.compiledVersion)
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   322
                    && Objects.equals(rawCompiledVersion, that.rawCompiledVersion);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   323
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   324
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   325
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   326
         * Computes a hash code for this module dependence.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   327
         *
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   328
         * <p> The hash code is based upon the module name, modifiers, and the
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   329
         * module version if recorded at compile time. It satisfies the general
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   330
         * contract of the {@link Object#hashCode Object.hashCode} method. </p>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   331
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   332
         * @return The hash-code value for this module dependence
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   333
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   334
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   335
        public int hashCode() {
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   336
            int hash = name.hashCode() * 43 + mods.hashCode();
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   337
            if (compiledVersion != null)
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   338
                hash = hash * 43 + compiledVersion.hashCode();
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   339
            if (rawCompiledVersion != null)
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   340
                hash = hash * 43 + rawCompiledVersion.hashCode();
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   341
            return hash;
36511
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
        /**
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   345
         * Returns a string describing this module dependence.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   346
         *
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   347
         * @return A string describing this module dependence
36511
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() {
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   351
            String what;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   352
            if (compiledVersion != null) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   353
                what = name() + " (@" + compiledVersion + ")";
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   354
            } else {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   355
                what = name();
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   356
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   357
            return ModuleDescriptor.toString(mods, what);
36511
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
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   363
    /**
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   364
     * <p> A package exported by a module, may be qualified or unqualified. </p>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   365
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   366
     * @see ModuleDescriptor#exports()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   367
     * @since 9
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   368
     * @spec JPMS
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   369
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   370
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   371
    public final static class Exports
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   372
        implements Comparable<Exports>
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   373
    {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   374
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   375
        /**
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   376
         * A modifier on an exported package.
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   377
         *
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   378
         * @see Exports#modifiers()
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   379
         * @since 9
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   380
         * @spec JPMS
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   381
         */
51327
a19fda433921 8209003: Consolidate use of empty collections in java.lang.module
redestad
parents: 49529
diff changeset
   382
        public enum Modifier {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   383
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   384
            /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   385
             * The export was not explicitly or implicitly declared in the
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   386
             * source of the module declaration.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   387
             */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   388
            SYNTHETIC,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   389
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   390
            /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   391
             * The export was implicitly declared in the source of the module
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   392
             * declaration.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   393
             */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   394
            MANDATED;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   395
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   396
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   397
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   398
        private final Set<Modifier> mods;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   399
        private final String source;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   400
        private final Set<String> targets;  // empty if unqualified export
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   401
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   402
        /**
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   403
         * Constructs an export
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   404
         */
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   405
        private Exports(Set<Modifier> ms, String source, Set<String> targets) {
51327
a19fda433921 8209003: Consolidate use of empty collections in java.lang.module
redestad
parents: 49529
diff changeset
   406
            this.mods = Set.copyOf(ms);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   407
            this.source = source;
51327
a19fda433921 8209003: Consolidate use of empty collections in java.lang.module
redestad
parents: 49529
diff changeset
   408
            this.targets = Set.copyOf(targets);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   409
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   410
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   411
        private Exports(Set<Modifier> ms,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   412
                        String source,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   413
                        Set<String> targets,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   414
                        boolean unused) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   415
            this.mods = ms;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   416
            this.source = source;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   417
            this.targets = targets;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   418
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   419
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   420
        /**
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   421
         * Returns the set of modifiers.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   422
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   423
         * @return A possibly-empty unmodifiable set of modifiers
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   424
         */
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   425
        public Set<Modifier> modifiers() {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   426
            return mods;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   427
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   428
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   429
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   430
         * Returns {@code true} if this is a qualified export.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   431
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   432
         * @return {@code true} if this is a qualified export
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   433
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   434
        public boolean isQualified() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   435
            return !targets.isEmpty();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   436
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   437
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   438
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   439
         * Returns the package name.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   440
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   441
         * @return The package name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   442
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   443
        public String source() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   444
            return source;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   445
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   446
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   447
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   448
         * For a qualified export, returns the non-empty and immutable set
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   449
         * of the module names to which the package is exported. For an
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   450
         * unqualified export, returns an empty set.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   451
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   452
         * @return The set of target module names or for an unqualified
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   453
         *         export, an empty set
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   454
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   455
        public Set<String> targets() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   456
            return targets;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   457
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   458
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   459
        /**
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   460
         * Compares this module export to another.
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   461
         *
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   462
         * <p> Two {@code Exports} objects are compared by comparing the package
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   463
         * names lexicographically. Where the packages names are equal then the
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   464
         * sets of modifiers are compared in the same way that module modifiers
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   465
         * are compared (see {@link ModuleDescriptor#compareTo
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   466
         * ModuleDescriptor.compareTo}). Where the package names are equal and
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   467
         * the set of modifiers are equal then the set of target modules are
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   468
         * compared. This is done by sorting the names of the target modules
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   469
         * in ascending order, and according to their natural ordering, and then
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   470
         * comparing the corresponding elements lexicographically. Where the
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   471
         * sets differ in size, and the larger set contains all elements of the
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   472
         * smaller set, then the larger set is considered to succeed the smaller
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   473
         * set. </p>
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   474
         *
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   475
         * @param  that
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   476
         *         The module export to compare
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   477
         *
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   478
         * @return A negative integer, zero, or a positive integer if this module
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   479
         *         export is less than, equal to, or greater than the given
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   480
         *         export dependence
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   481
         */
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   482
        @Override
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   483
        public int compareTo(Exports that) {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   484
            if (this == that) return 0;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   485
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   486
            int c = source.compareTo(that.source);
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   487
            if (c != 0)
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   488
                return c;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   489
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   490
            // modifiers
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   491
            long v1 = modsValue(this.modifiers());
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   492
            long v2 = modsValue(that.modifiers());
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   493
            c = Long.compare(v1, v2);
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   494
            if (c != 0)
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   495
                return c;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   496
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   497
            // targets
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   498
            c = compare(targets, that.targets);
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   499
            if (c != 0)
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   500
                return c;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   501
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   502
            return 0;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   503
        }
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   504
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   505
        /**
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   506
         * Computes a hash code for this module export.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   507
         *
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   508
         * <p> The hash code is based upon the modifiers, the package name,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   509
         * and for a qualified export, the set of modules names to which the
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   510
         * package is exported. It satisfies the general contract of the
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   511
         * {@link Object#hashCode Object.hashCode} method.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   512
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   513
         * @return The hash-code value for this module export
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   514
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   515
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   516
        public int hashCode() {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   517
            int hash = mods.hashCode();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   518
            hash = hash * 43 + source.hashCode();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   519
            return hash * 43 + targets.hashCode();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   520
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   521
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   522
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   523
         * Tests this module export for equality with the given object.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   524
         *
37880
60ec48925dc6 8156661: Handful of typos in javadoc
igerasim
parents: 37779
diff changeset
   525
         * <p> If the given object is not an {@code Exports} then this method
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   526
         * returns {@code false}. Two module exports objects are equal if their
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   527
         * set of modifiers is equal, the package names are equal and the set
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   528
         * of target module names is equal. </p>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   529
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   530
         * <p> This method satisfies the general contract of the {@link
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   531
         * java.lang.Object#equals(Object) Object.equals} method. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   532
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   533
         * @param   ob
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   534
         *          the object to which this object is to be compared
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   535
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   536
         * @return  {@code true} if, and only if, the given object is a module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   537
         *          dependence that is equal to this module dependence
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   538
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   539
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   540
        public boolean equals(Object ob) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   541
            if (!(ob instanceof Exports))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   542
                return false;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   543
            Exports other = (Exports)ob;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   544
            return Objects.equals(this.mods, other.mods)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   545
                    && Objects.equals(this.source, other.source)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   546
                    && Objects.equals(this.targets, other.targets);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   547
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   548
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   549
        /**
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   550
         * Returns a string describing the exported package.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   551
         *
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   552
         * @return A string describing the exported package
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   553
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   554
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   555
        public String toString() {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   556
            String s = ModuleDescriptor.toString(mods, source);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   557
            if (targets.isEmpty())
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   558
                return s;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   559
            else
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   560
                return s + " to " + targets;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   561
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   562
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   563
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   564
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   565
    /**
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   566
     * <p> A package opened by a module, may be qualified or unqualified. </p>
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   567
     *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   568
     * <p> The <em>opens</em> directive in a module declaration declares a
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   569
     * package to be open to allow all types in the package, and all their
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   570
     * members, not just public types and their public members to be reflected
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   571
     * on by APIs that support private access or a way to bypass or suppress
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   572
     * default Java language access control checks. </p>
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   573
     *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   574
     * @see ModuleDescriptor#opens()
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   575
     * @since 9
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   576
     * @spec JPMS
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   577
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   578
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   579
    public final static class Opens
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   580
        implements Comparable<Opens>
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   581
    {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   582
        /**
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   583
         * A modifier on an open package.
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   584
         *
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   585
         * @see Opens#modifiers()
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   586
         * @since 9
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   587
         * @spec JPMS
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   588
         */
51327
a19fda433921 8209003: Consolidate use of empty collections in java.lang.module
redestad
parents: 49529
diff changeset
   589
        public enum Modifier {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   590
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   591
            /**
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   592
             * The open package was not explicitly or implicitly declared in
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   593
             * the source of the module declaration.
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   594
             */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   595
            SYNTHETIC,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   596
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   597
            /**
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   598
             * The open package was implicitly declared in the source of the
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   599
             * module declaration.
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   600
             */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   601
            MANDATED;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   602
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   603
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   604
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   605
        private final Set<Modifier> mods;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   606
        private final String source;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   607
        private final Set<String> targets;  // empty if unqualified export
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   608
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   609
        /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   610
         * Constructs an Opens
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   611
         */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   612
        private Opens(Set<Modifier> ms, String source, Set<String> targets) {
51327
a19fda433921 8209003: Consolidate use of empty collections in java.lang.module
redestad
parents: 49529
diff changeset
   613
            this.mods = Set.copyOf(ms);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   614
            this.source = source;
51327
a19fda433921 8209003: Consolidate use of empty collections in java.lang.module
redestad
parents: 49529
diff changeset
   615
            this.targets = Set.copyOf(targets);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   616
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   617
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   618
        private Opens(Set<Modifier> ms,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   619
                      String source,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   620
                      Set<String> targets,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   621
                      boolean unused) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   622
            this.mods = ms;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   623
            this.source = source;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   624
            this.targets = targets;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   625
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   626
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   627
        /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   628
         * Returns the set of modifiers.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   629
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   630
         * @return A possibly-empty unmodifiable set of modifiers
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   631
         */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   632
        public Set<Modifier> modifiers() {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   633
            return mods;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   634
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   635
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   636
        /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   637
         * Returns {@code true} if this is a qualified opens.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   638
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   639
         * @return {@code true} if this is a qualified opens
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   640
         */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   641
        public boolean isQualified() {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   642
            return !targets.isEmpty();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   643
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   644
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   645
        /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   646
         * Returns the package name.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   647
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   648
         * @return The package name
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   649
         */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   650
        public String source() {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   651
            return source;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   652
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   653
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   654
        /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   655
         * For a qualified opens, returns the non-empty and immutable set
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   656
         * of the module names to which the package is open. For an
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   657
         * unqualified opens, returns an empty set.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   658
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   659
         * @return The set of target module names or for an unqualified
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   660
         *         opens, an empty set
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   661
         */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   662
        public Set<String> targets() {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   663
            return targets;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   664
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   665
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   666
        /**
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   667
         * Compares this module opens to another.
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   668
         *
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   669
         * <p> Two {@code Opens} objects are compared by comparing the package
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   670
         * names lexicographically. Where the packages names are equal then the
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   671
         * sets of modifiers are compared in the same way that module modifiers
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   672
         * are compared (see {@link ModuleDescriptor#compareTo
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   673
         * ModuleDescriptor.compareTo}). Where the package names are equal and
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   674
         * the set of modifiers are equal then the set of target modules are
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   675
         * compared. This is done by sorting the names of the target modules
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   676
         * in ascending order, and according to their natural ordering, and then
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   677
         * comparing the corresponding elements lexicographically. Where the
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   678
         * sets differ in size, and the larger set contains all elements of the
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   679
         * smaller set, then the larger set is considered to succeed the smaller
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   680
         * set. </p>
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   681
         *
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   682
         * @param  that
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   683
         *         The module opens to compare
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   684
         *
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   685
         * @return A negative integer, zero, or a positive integer if this module
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   686
         *         opens is less than, equal to, or greater than the given
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   687
         *         module opens
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   688
         */
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   689
        @Override
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   690
        public int compareTo(Opens that) {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   691
            if (this == that) return 0;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   692
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   693
            int c = source.compareTo(that.source);
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   694
            if (c != 0)
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   695
                return c;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   696
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   697
            // modifiers
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   698
            long v1 = modsValue(this.modifiers());
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   699
            long v2 = modsValue(that.modifiers());
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   700
            c = Long.compare(v1, v2);
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   701
            if (c != 0)
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   702
                return c;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   703
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   704
            // targets
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   705
            c = compare(targets, that.targets);
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   706
            if (c != 0)
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   707
                return c;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   708
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   709
            return 0;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   710
        }
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   711
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   712
        /**
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   713
         * Computes a hash code for this module opens.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   714
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   715
         * <p> The hash code is based upon the modifiers, the package name,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   716
         * and for a qualified opens, the set of modules names to which the
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   717
         * package is opened. It satisfies the general contract of the
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   718
         * {@link Object#hashCode Object.hashCode} method.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   719
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   720
         * @return The hash-code value for this module opens
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   721
         */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   722
        @Override
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   723
        public int hashCode() {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   724
            int hash = mods.hashCode();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   725
            hash = hash * 43 + source.hashCode();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   726
            return hash * 43 + targets.hashCode();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   727
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   728
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   729
        /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   730
         * Tests this module opens for equality with the given object.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   731
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   732
         * <p> If the given object is not an {@code Opens} then this method
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   733
         * returns {@code false}. Two {@code Opens} objects are equal if their
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   734
         * set of modifiers is equal, the package names are equal and the set
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   735
         * of target module names is equal. </p>
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   736
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   737
         * <p> This method satisfies the general contract of the {@link
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   738
         * java.lang.Object#equals(Object) Object.equals} method. </p>
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   739
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   740
         * @param   ob
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   741
         *          the object to which this object is to be compared
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   742
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   743
         * @return  {@code true} if, and only if, the given object is a module
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   744
         *          dependence that is equal to this module dependence
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   745
         */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   746
        @Override
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   747
        public boolean equals(Object ob) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   748
            if (!(ob instanceof Opens))
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   749
                return false;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   750
            Opens other = (Opens)ob;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   751
            return Objects.equals(this.mods, other.mods)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   752
                    && Objects.equals(this.source, other.source)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   753
                    && Objects.equals(this.targets, other.targets);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   754
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   755
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   756
        /**
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   757
         * Returns a string describing the open package.
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   758
         *
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   759
         * @return A string describing the open package
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   760
         */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   761
        @Override
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   762
        public String toString() {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   763
            String s = ModuleDescriptor.toString(mods, source);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   764
            if (targets.isEmpty())
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   765
                return s;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   766
            else
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   767
                return s + " to " + targets;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   768
        }
36511
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
     * <p> A service that a module provides one or more implementations of. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   774
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   775
     * @see ModuleDescriptor#provides()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   776
     * @since 9
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   777
     * @spec JPMS
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   778
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   779
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   780
    public final static class Provides
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   781
        implements Comparable<Provides>
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   782
    {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   783
        private final String service;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   784
        private final List<String> providers;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   785
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   786
        private Provides(String service, List<String> providers) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   787
            this.service = service;
51327
a19fda433921 8209003: Consolidate use of empty collections in java.lang.module
redestad
parents: 49529
diff changeset
   788
            this.providers = List.copyOf(providers);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   789
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   790
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   791
        private Provides(String service, List<String> providers, boolean unused) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   792
            this.service = service;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   793
            this.providers = providers;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   794
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   795
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   796
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   797
         * Returns the fully qualified class name of the service type.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   798
         *
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   799
         * @return The fully qualified class name of the service type
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   800
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   801
        public String service() { return service; }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   802
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   803
        /**
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   804
         * Returns the list of the fully qualified class names of the providers
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   805
         * or provider factories.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   806
         *
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   807
         * @return A non-empty and unmodifiable list of the fully qualified class
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   808
         *         names of the providers or provider factories
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   809
         */
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   810
        public List<String> providers() { return providers; }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   811
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   812
        /**
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   813
         * Compares this provides to another.
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   814
         *
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   815
         * <p> Two {@code Provides} objects are compared by comparing the fully
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   816
         * qualified class name of the service type lexicographically. Where the
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   817
         * class names are equal then the list of the provider class names are
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   818
         * compared by comparing the corresponding elements of both lists
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   819
         * lexicographically and in sequence. Where the lists differ in size,
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   820
         * {@code N} is the size of the shorter list, and the first {@code N}
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   821
         * corresponding elements are equal, then the longer list is considered
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   822
         * to succeed the shorter list. </p>
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   823
         *
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   824
         * @param  that
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   825
         *         The {@code Provides} to compare
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   826
         *
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   827
         * @return A negative integer, zero, or a positive integer if this provides
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   828
         *         is less than, equal to, or greater than the given provides
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   829
         */
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   830
        public int compareTo(Provides that) {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   831
            if (this == that) return 0;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   832
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   833
            int c = service.compareTo(that.service);
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   834
            if (c != 0) return c;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   835
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   836
            // compare provider class names in sequence
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   837
            int size1 = this.providers.size();
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   838
            int size2 = that.providers.size();
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   839
            for (int index=0; index<Math.min(size1, size2); index++) {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   840
                String e1 = this.providers.get(index);
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   841
                String e2 = that.providers.get(index);
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   842
                c = e1.compareTo(e2);
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   843
                if (c != 0) return c;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   844
            }
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   845
            if (size1 == size2) {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   846
                return 0;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   847
            } else {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   848
                return (size1 > size2) ? 1 : -1;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   849
            }
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   850
        }
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   851
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   852
        /**
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   853
         * Computes a hash code for this provides.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   854
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   855
         * <p> The hash code is based upon the service type and the set of
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   856
         * providers. It satisfies the general contract of the {@link
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   857
         * Object#hashCode Object.hashCode} method. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   858
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   859
         * @return The hash-code value for this module provides
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   860
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   861
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   862
        public int hashCode() {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   863
            return service.hashCode() * 43 + providers.hashCode();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   864
        }
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
         * Tests this provides for equality with the given object.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   868
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   869
         * <p> If the given object is not a {@code Provides} then this method
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   870
         * returns {@code false}. Two {@code Provides} objects are equal if the
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   871
         * service type is equal and the list of providers is equal. </p>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   872
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   873
         * <p> This method satisfies the general contract of the {@link
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   874
         * java.lang.Object#equals(Object) Object.equals} method. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   875
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   876
         * @param   ob
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   877
         *          the object to which this object is to be compared
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   878
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   879
         * @return  {@code true} if, and only if, the given object is a
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   880
         *          {@code Provides} that is equal to this {@code Provides}
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   881
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   882
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   883
        public boolean equals(Object ob) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   884
            if (!(ob instanceof Provides))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   885
                return false;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   886
            Provides other = (Provides)ob;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   887
            return Objects.equals(this.service, other.service) &&
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   888
                    Objects.equals(this.providers, other.providers);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   889
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   890
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   891
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   892
         * Returns a string describing this provides.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   893
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   894
         * @return A string describing this provides
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   895
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   896
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   897
        public String toString() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   898
            return service + " with " + providers;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   899
        }
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

9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   904
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   905
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   906
     * A module's version string.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   907
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   908
     * <p> A version string has three components: The version number itself, an
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   909
     * optional pre-release version, and an optional build version.  Each
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   910
     * component is a sequence of tokens; each token is either a non-negative
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   911
     * integer or a string.  Tokens are separated by the punctuation characters
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   912
     * {@code '.'}, {@code '-'}, or {@code '+'}, or by transitions from a
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   913
     * sequence of digits to a sequence of characters that are neither digits
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   914
     * nor punctuation characters, or vice versa.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   915
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   916
     * <ul>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   917
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   918
     *   <li> The <i>version number</i> is a sequence of tokens separated by
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   919
     *   {@code '.'} characters, terminated by the first {@code '-'} or {@code
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   920
     *   '+'} character. </li>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   921
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   922
     *   <li> The <i>pre-release version</i> is a sequence of tokens separated
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   923
     *   by {@code '.'} or {@code '-'} characters, terminated by the first
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   924
     *   {@code '+'} character. </li>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   925
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   926
     *   <li> The <i>build version</i> is a sequence of tokens separated by
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   927
     *   {@code '.'}, {@code '-'}, or {@code '+'} characters.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   928
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   929
     * </ul>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   930
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   931
     * <p> When comparing two version strings, the elements of their
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   932
     * corresponding components are compared in pointwise fashion.  If one
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   933
     * component is longer than the other, but otherwise equal to it, then the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   934
     * first component is considered the greater of the two; otherwise, if two
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   935
     * corresponding elements are integers then they are compared as such;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   936
     * otherwise, at least one of the elements is a string, so the other is
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   937
     * converted into a string if it is an integer and the two are compared
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   938
     * lexicographically.  Trailing integer elements with the value zero are
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   939
     * ignored.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   940
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   941
     * <p> Given two version strings, if their version numbers differ then the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   942
     * result of comparing them is the result of comparing their version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   943
     * numbers; otherwise, if one of them has a pre-release version but the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   944
     * other does not then the first is considered to precede the second,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   945
     * otherwise the result of comparing them is the result of comparing their
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   946
     * pre-release versions; otherwise, the result of comparing them is the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   947
     * result of comparing their build versions.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   948
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   949
     * @see ModuleDescriptor#version()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   950
     * @since 9
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   951
     * @spec JPMS
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   952
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   953
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   954
    public final static class Version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   955
        implements Comparable<Version>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   956
    {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   957
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   958
        private final String version;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   959
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   960
        // If Java had disjunctive types then we'd write List<Integer|String> here
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   961
        //
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   962
        private final List<Object> sequence;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   963
        private final List<Object> pre;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   964
        private final List<Object> build;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   965
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   966
        // Take a numeric token starting at position i
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   967
        // Append it to the given list
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   968
        // Return the index of the first character not taken
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   969
        // Requires: s.charAt(i) is (decimal) numeric
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   970
        //
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   971
        private static int takeNumber(String s, int i, List<Object> acc) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   972
            char c = s.charAt(i);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   973
            int d = (c - '0');
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   974
            int n = s.length();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   975
            while (++i < n) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   976
                c = s.charAt(i);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   977
                if (c >= '0' && c <= '9') {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   978
                    d = d * 10 + (c - '0');
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   979
                    continue;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   980
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   981
                break;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   982
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   983
            acc.add(d);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   984
            return i;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   985
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   986
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   987
        // Take a string token starting at position i
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   988
        // Append it to the given list
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   989
        // Return the index of the first character not taken
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   990
        // Requires: s.charAt(i) is not '.'
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   991
        //
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   992
        private static int takeString(String s, int i, List<Object> acc) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   993
            int b = i;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   994
            int n = s.length();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   995
            while (++i < n) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   996
                char c = s.charAt(i);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   997
                if (c != '.' && c != '-' && c != '+' && !(c >= '0' && c <= '9'))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   998
                    continue;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   999
                break;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1000
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1001
            acc.add(s.substring(b, i));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1002
            return i;
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
        // Syntax: tok+ ( '-' tok+)? ( '+' tok+)?
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1006
        // First token string is sequence, second is pre, third is build
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1007
        // Tokens are separated by '.' or '-', or by changes between alpha & numeric
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1008
        // Numeric tokens are compared as decimal integers
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1009
        // Non-numeric tokens are compared lexicographically
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1010
        // 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
  1011
        // Tokens in build may contain '-' and '+'
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1012
        //
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1013
        private Version(String v) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1014
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1015
            if (v == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1016
                throw new IllegalArgumentException("Null version string");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1017
            int n = v.length();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1018
            if (n == 0)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1019
                throw new IllegalArgumentException("Empty version string");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1020
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1021
            int i = 0;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1022
            char c = v.charAt(i);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1023
            if (!(c >= '0' && c <= '9'))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1024
                throw new IllegalArgumentException(v
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1025
                                                   + ": Version string does not start"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1026
                                                   + " with a number");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1027
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1028
            List<Object> sequence = new ArrayList<>(4);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1029
            List<Object> pre = new ArrayList<>(2);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1030
            List<Object> build = new ArrayList<>(2);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1031
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1032
            i = takeNumber(v, i, sequence);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1033
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1034
            while (i < n) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1035
                c = v.charAt(i);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1036
                if (c == '.') {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1037
                    i++;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1038
                    continue;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1039
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1040
                if (c == '-' || c == '+') {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1041
                    i++;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1042
                    break;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1043
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1044
                if (c >= '0' && c <= '9')
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1045
                    i = takeNumber(v, i, sequence);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1046
                else
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1047
                    i = takeString(v, i, sequence);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1048
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1049
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1050
            if (c == '-' && i >= n)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1051
                throw new IllegalArgumentException(v + ": Empty pre-release");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1052
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1053
            while (i < n) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1054
                c = v.charAt(i);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1055
                if (c >= '0' && c <= '9')
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1056
                    i = takeNumber(v, i, pre);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1057
                else
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1058
                    i = takeString(v, i, pre);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1059
                if (i >= n)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1060
                    break;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1061
                c = v.charAt(i);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1062
                if (c == '.' || c == '-') {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1063
                    i++;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1064
                    continue;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1065
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1066
                if (c == '+') {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1067
                    i++;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1068
                    break;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1069
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1070
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1071
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1072
            if (c == '+' && i >= n)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1073
                throw new IllegalArgumentException(v + ": Empty pre-release");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1074
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1075
            while (i < n) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1076
                c = v.charAt(i);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1077
                if (c >= '0' && c <= '9')
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1078
                    i = takeNumber(v, i, build);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1079
                else
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1080
                    i = takeString(v, i, build);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1081
                if (i >= n)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1082
                    break;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1083
                c = v.charAt(i);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1084
                if (c == '.' || c == '-' || c == '+') {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1085
                    i++;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1086
                    continue;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1087
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1088
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1089
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1090
            this.version = v;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1091
            this.sequence = sequence;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1092
            this.pre = pre;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1093
            this.build = build;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1094
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1095
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1096
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1097
         * Parses the given string as a version string.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1098
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1099
         * @param  v
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1100
         *         The string to parse
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1101
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1102
         * @return The resulting {@code Version}
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1103
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1104
         * @throws IllegalArgumentException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1105
         *         If {@code v} is {@code null}, an empty string, or cannot be
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1106
         *         parsed as a version string
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1107
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1108
        public static Version parse(String v) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1109
            return new Version(v);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1110
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1111
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1112
        @SuppressWarnings("unchecked")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1113
        private int cmp(Object o1, Object o2) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1114
            return ((Comparable)o1).compareTo(o2);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1115
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1116
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1117
        private int compareTokens(List<Object> ts1, List<Object> ts2) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1118
            int n = Math.min(ts1.size(), ts2.size());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1119
            for (int i = 0; i < n; i++) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1120
                Object o1 = ts1.get(i);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1121
                Object o2 = ts2.get(i);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1122
                if ((o1 instanceof Integer && o2 instanceof Integer)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1123
                    || (o1 instanceof String && o2 instanceof String))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1124
                {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1125
                    int c = cmp(o1, o2);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1126
                    if (c == 0)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1127
                        continue;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1128
                    return c;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1129
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1130
                // Types differ, so convert number to string form
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1131
                int c = o1.toString().compareTo(o2.toString());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1132
                if (c == 0)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1133
                    continue;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1134
                return c;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1135
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1136
            List<Object> rest = ts1.size() > ts2.size() ? ts1 : ts2;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1137
            int e = rest.size();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1138
            for (int i = n; i < e; i++) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1139
                Object o = rest.get(i);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1140
                if (o instanceof Integer && ((Integer)o) == 0)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1141
                    continue;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1142
                return ts1.size() - ts2.size();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1143
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1144
            return 0;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1145
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1146
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1147
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1148
         * Compares this module version to another module version. Module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1149
         * versions are compared as described in the class description.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1150
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1151
         * @param that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1152
         *        The module version to compare
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1153
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1154
         * @return A negative integer, zero, or a positive integer as this
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1155
         *         module version is less than, equal to, or greater than the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1156
         *         given module version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1157
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1158
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1159
        public int compareTo(Version that) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1160
            int c = compareTokens(this.sequence, that.sequence);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1161
            if (c != 0) return c;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1162
            if (this.pre.isEmpty()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1163
                if (!that.pre.isEmpty()) return +1;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1164
            } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1165
                if (that.pre.isEmpty()) return -1;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1166
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1167
            c = compareTokens(this.pre, that.pre);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1168
            if (c != 0) return c;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1169
            return compareTokens(this.build, that.build);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1170
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1171
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1172
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1173
         * Tests this module version for equality with the given object.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1174
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1175
         * <p> If the given object is not a {@code Version} then this method
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1176
         * returns {@code false}. Two module version are equal if their
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1177
         * corresponding components are equal. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1178
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1179
         * <p> This method satisfies the general contract of the {@link
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1180
         * java.lang.Object#equals(Object) Object.equals} method. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1181
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1182
         * @param   ob
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1183
         *          the object to which this object is to be compared
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1184
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1185
         * @return  {@code true} if, and only if, the given object is a module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1186
         *          reference that is equal to this module reference
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1187
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1188
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1189
        public boolean equals(Object ob) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1190
            if (!(ob instanceof Version))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1191
                return false;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1192
            return compareTo((Version)ob) == 0;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1193
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1194
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1195
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1196
         * Computes a hash code for this module version.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1197
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1198
         * <p> The hash code is based upon the components of the version and
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1199
         * satisfies the general contract of the {@link Object#hashCode
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1200
         * Object.hashCode} method. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1201
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1202
         * @return The hash-code value for this module version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1203
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1204
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1205
        public int hashCode() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1206
            return version.hashCode();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1207
        }
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
         * Returns the string from which this version was parsed.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1211
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1212
         * @return The string from which this version was parsed.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1213
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1214
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1215
        public String toString() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1216
            return version;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1217
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1218
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1219
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1220
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1221
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1222
    private final String name;
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1223
    private final Version version;
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1224
    private final String rawVersionString;
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1225
    private final Set<Modifier> modifiers;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1226
    private final boolean open;  // true if modifiers contains OPEN
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1227
    private final boolean automatic;  // true if modifiers contains AUTOMATIC
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1228
    private final Set<Requires> requires;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1229
    private final Set<Exports> exports;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1230
    private final Set<Opens> opens;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1231
    private final Set<String> uses;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1232
    private final Set<Provides> provides;
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1233
    private final Set<String> packages;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1234
    private final String mainClass;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1235
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1236
    private ModuleDescriptor(String name,
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1237
                             Version version,
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1238
                             String rawVersionString,
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1239
                             Set<Modifier> modifiers,
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1240
                             Set<Requires> requires,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1241
                             Set<Exports> exports,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1242
                             Set<Opens> opens,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1243
                             Set<String> uses,
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1244
                             Set<Provides> provides,
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1245
                             Set<String> packages,
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1246
                             String mainClass)
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1247
    {
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1248
        assert version == null || rawVersionString == null;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1249
        this.name = name;
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1250
        this.version = version;
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1251
        this.rawVersionString = rawVersionString;
51327
a19fda433921 8209003: Consolidate use of empty collections in java.lang.module
redestad
parents: 49529
diff changeset
  1252
        this.modifiers = Set.copyOf(modifiers);
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1253
        this.open = modifiers.contains(Modifier.OPEN);
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1254
        this.automatic = modifiers.contains(Modifier.AUTOMATIC);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1255
        assert (requires.stream().map(Requires::name).distinct().count()
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1256
                == requires.size());
51327
a19fda433921 8209003: Consolidate use of empty collections in java.lang.module
redestad
parents: 49529
diff changeset
  1257
        this.requires = Set.copyOf(requires);
a19fda433921 8209003: Consolidate use of empty collections in java.lang.module
redestad
parents: 49529
diff changeset
  1258
        this.exports = Set.copyOf(exports);
a19fda433921 8209003: Consolidate use of empty collections in java.lang.module
redestad
parents: 49529
diff changeset
  1259
        this.opens = Set.copyOf(opens);
a19fda433921 8209003: Consolidate use of empty collections in java.lang.module
redestad
parents: 49529
diff changeset
  1260
        this.uses = Set.copyOf(uses);
a19fda433921 8209003: Consolidate use of empty collections in java.lang.module
redestad
parents: 49529
diff changeset
  1261
        this.provides = Set.copyOf(provides);
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1262
51327
a19fda433921 8209003: Consolidate use of empty collections in java.lang.module
redestad
parents: 49529
diff changeset
  1263
        this.packages = Set.copyOf(packages);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1264
        this.mainClass = mainClass;
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
    /**
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1268
     * Creates a module descriptor from its components.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1269
     * The arguments are pre-validated and sets are unmodifiable sets.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1270
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1271
    ModuleDescriptor(String name,
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1272
                     Version version,
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1273
                     Set<Modifier> modifiers,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1274
                     Set<Requires> requires,
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1275
                     Set<Exports> exports,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1276
                     Set<Opens> opens,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1277
                     Set<String> uses,
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1278
                     Set<Provides> provides,
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1279
                     Set<String> packages,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1280
                     String mainClass,
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1281
                     int hashCode,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1282
                     boolean unused) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1283
        this.name = name;
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1284
        this.version = version;
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1285
        this.rawVersionString = null;
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1286
        this.modifiers = modifiers;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1287
        this.open = modifiers.contains(Modifier.OPEN);
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1288
        this.automatic = modifiers.contains(Modifier.AUTOMATIC);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1289
        this.requires = requires;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1290
        this.exports = exports;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1291
        this.opens = opens;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1292
        this.uses = uses;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1293
        this.provides = provides;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1294
        this.packages = packages;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1295
        this.mainClass = mainClass;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1296
        this.hash = hashCode;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1297
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1298
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1299
    /**
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1300
     * <p> Returns the module name. </p>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1301
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1302
     * @return The module name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1303
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1304
    public String name() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1305
        return name;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1306
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1307
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1308
    /**
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1309
     * <p> Returns the set of module modifiers. </p>
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1310
     *
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1311
     * @return A possibly-empty unmodifiable set of modifiers
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1312
     */
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1313
    public Set<Modifier> modifiers() {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1314
        return modifiers;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1315
    }
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1316
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1317
    /**
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1318
     * <p> Returns {@code true} if this is an open module. </p>
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1319
     *
47004
b7e72fc752c9 8186684: Fix broken links in java.base API docs
jjg
parents: 46096
diff changeset
  1320
     * <p> This method is equivalent to testing if the set of {@link #modifiers()
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1321
     * modifiers} contains the {@link Modifier#OPEN OPEN} modifier. </p>
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1322
     *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1323
     * @return  {@code true} if this is an open module
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1324
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1325
    public boolean isOpen() {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1326
        return open;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1327
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1328
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1329
    /**
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1330
     * <p> Returns {@code true} if this is an automatic module. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1331
     *
47004
b7e72fc752c9 8186684: Fix broken links in java.base API docs
jjg
parents: 46096
diff changeset
  1332
     * <p> This method is equivalent to testing if the set of {@link #modifiers()
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1333
     * modifiers} contains the {@link Modifier#OPEN AUTOMATIC} modifier. </p>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1334
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1335
     * @return  {@code true} if this is an automatic module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1336
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1337
    public boolean isAutomatic() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1338
        return automatic;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1339
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1340
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1341
    /**
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1342
     * <p> Returns the set of {@code Requires} objects representing the module
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1343
     * dependences. </p>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1344
     *
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1345
     * <p> The set includes a dependency on "{@code java.base}" when this
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1346
     * module is not named "{@code java.base}". If this module is an automatic
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1347
     * module then it does not have a dependency on any module other than
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1348
     * "{@code java.base}". </p>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1349
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1350
     * @return  A possibly-empty unmodifiable set of {@link Requires} objects
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1351
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1352
    public Set<Requires> requires() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1353
        return requires;
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
    /**
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1357
     * <p> Returns the set of {@code Exports} objects representing the exported
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1358
     * packages. </p>
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1359
     *
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1360
     * <p> If this module is an automatic module then the set of exports
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1361
     * is empty. </p>
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1362
     *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1363
     * @return  A possibly-empty unmodifiable set of exported packages
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1364
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1365
    public Set<Exports> exports() {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1366
        return exports;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1367
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1368
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1369
    /**
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1370
     * <p> Returns the set of {@code Opens} objects representing the open
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1371
     * packages. </p>
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1372
     *
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1373
     * <p> If this module is an open module or an automatic module then the
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1374
     * set of open packages is empty. </p>
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1375
     *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1376
     * @return  A possibly-empty unmodifiable set of open packages
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1377
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1378
    public Set<Opens> opens() {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1379
        return opens;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1380
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1381
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1382
    /**
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1383
     * <p> Returns the set of service dependences. </p>
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1384
     *
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1385
     * <p> If this module is an automatic module then the set of service
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1386
     * dependences is empty. </p>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1387
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1388
     * @return  A possibly-empty unmodifiable set of the fully qualified class
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1389
     *          names of the service types used
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1390
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1391
    public Set<String> uses() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1392
        return uses;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1393
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1394
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1395
    /**
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1396
     * <p> Returns the set of {@code Provides} objects representing the
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1397
     * services that the module provides. </p>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1398
     *
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1399
     * @return The possibly-empty unmodifiable set of the services that this
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1400
     *         module provides
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1401
     */
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1402
    public Set<Provides> provides() {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1403
        return provides;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1404
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1405
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1406
    /**
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1407
     * <p> Returns the module version. </p>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1408
     *
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1409
     * @return This module's version, or an empty {@code Optional} if the
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1410
     *         module does not have a version or the version is
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1411
     *         {@linkplain Version#parse(String) unparseable}
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1412
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1413
    public Optional<Version> version() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1414
        return Optional.ofNullable(version);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1415
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1416
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1417
    /**
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1418
     * <p> Returns the string with the possibly-unparseable version of the
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1419
     * module </p>
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1420
     *
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1421
     * @return The string containing the version of the module or an empty
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1422
     *         {@code Optional} if the module does not have a version
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1423
     *
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1424
     * @see #version()
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1425
     */
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1426
    public Optional<String> rawVersion() {
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1427
        if (version != null) {
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1428
            return Optional.of(version.toString());
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1429
        } else {
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1430
            return Optional.ofNullable(rawVersionString);
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1431
        }
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1432
    }
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1433
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1434
    /**
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1435
     * <p> Returns a string containing the module name and, if present, its
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1436
     * version. </p>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1437
     *
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1438
     * @return A string containing the module name and, if present, its
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1439
     *         version
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1440
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1441
    public String toNameAndVersion() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1442
        if (version != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1443
            return name() + "@" + version;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1444
        } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1445
            return name();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1446
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1447
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1448
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1449
    /**
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1450
     * <p> Returns the module main class. </p>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1451
     *
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1452
     * @return The fully qualified class name of the module's main class
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1453
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1454
    public Optional<String> mainClass() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1455
        return Optional.ofNullable(mainClass);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1456
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1457
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1458
    /**
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1459
     * Returns the set of packages in the module.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1460
     *
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1461
     * <p> The set of packages includes all exported and open packages, as well
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1462
     * as the packages of any service providers, and the package for the main
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1463
     * class. </p>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1464
     *
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1465
     * @return A possibly-empty unmodifiable set of the packages in the module
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1466
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1467
    public Set<String> packages() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1468
        return packages;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1469
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1470
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1471
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1472
    /**
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1473
     * A builder for building {@link ModuleDescriptor} objects.
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1474
     *
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1475
     * <p> {@code ModuleDescriptor} defines the {@link #newModule newModule},
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1476
     * {@link #newOpenModule newOpenModule}, and {@link #newAutomaticModule
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1477
     * newAutomaticModule} methods to create builders for building
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1478
     * <em>normal</em>, open, and automatic modules. </p>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1479
     *
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1480
     * <p> The set of packages in the module are accumulated by the {@code
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1481
     * Builder} as the {@link ModuleDescriptor.Builder#exports(String) exports},
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1482
     * {@link ModuleDescriptor.Builder#opens(String) opens},
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1483
     * {@link ModuleDescriptor.Builder#packages(Set) packages},
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1484
     * {@link ModuleDescriptor.Builder#provides(String,List) provides}, and
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1485
     * {@link ModuleDescriptor.Builder#mainClass(String) mainClass} methods are
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1486
     * invoked. </p>
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1487
     *
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1488
     * <p> The module names, package names, and class names that are parameters
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1489
     * specified to the builder methods are the module names, package names,
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1490
     * and qualified names of classes (in named packages) as defined in the
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1491
     * <cite>The Java&trade; Language Specification</cite>. </p>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1492
     *
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1493
     * <p> Example usage: </p>
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1494
     * <pre>{@code    ModuleDescriptor descriptor = ModuleDescriptor.newModule("stats.core")
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1495
     *         .requires("java.base")
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1496
     *         .exports("org.acme.stats.core.clustering")
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1497
     *         .exports("org.acme.stats.core.regression")
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1498
     *         .packages(Set.of("org.acme.stats.core.internal"))
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1499
     *         .build();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1500
     * }</pre>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1501
     *
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1502
     * @apiNote A {@code Builder} checks the components and invariants as
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1503
     * components are added to the builder. The rationale for this is to detect
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1504
     * errors as early as possible and not defer all validation to the
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1505
     * {@link #build build} method.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1506
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1507
     * @since 9
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1508
     * @spec JPMS
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1509
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1510
    public static final class Builder {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1511
        final String name;
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1512
        final boolean strict;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1513
        final Set<Modifier> modifiers;
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1514
        final boolean open;
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1515
        final boolean automatic;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1516
        final Set<String> packages = new HashSet<>();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1517
        final Map<String, Requires> requires = new HashMap<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1518
        final Map<String, Exports> exports = new HashMap<>();
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1519
        final Map<String, Opens> opens = new HashMap<>();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1520
        final Set<String> uses = new HashSet<>();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1521
        final Map<String, Provides> provides = new HashMap<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1522
        Version version;
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1523
        String rawVersionString;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1524
        String mainClass;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1525
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1526
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1527
         * Initializes a new builder with the given module name.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1528
         *
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1529
         * If {@code strict} is {@code true} then module, package, and class
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1530
         * names are checked to ensure they are legal names. In addition, the
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1531
         * {@link #build buid} method will add "{@code requires java.base}" if
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1532
         * the dependency is not declared.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1533
         */
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1534
        Builder(String name, boolean strict, Set<Modifier> modifiers) {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1535
            this.name = (strict) ? requireModuleName(name) : name;
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1536
            this.strict = strict;
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1537
            this.modifiers = modifiers;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1538
            this.open = modifiers.contains(Modifier.OPEN);
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1539
            this.automatic = modifiers.contains(Modifier.AUTOMATIC);
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1540
            assert !open || !automatic;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1541
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1542
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1543
        /**
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1544
         * Returns a snapshot of the packages in the module.
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1545
         */
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1546
        /* package */ Set<String> packages() {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1547
            return Collections.unmodifiableSet(packages);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1548
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1549
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1550
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1551
         * Adds a dependence on a module.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1552
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1553
         * @param  req
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1554
         *         The dependence
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 the dependence is on the module that this builder was
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1560
         *         initialized to build
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1561
         * @throws IllegalStateException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1562
         *         If the dependence on the module has already been declared
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1563
         *         or this builder is for an automatic module
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1564
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1565
        public Builder requires(Requires req) {
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1566
            if (automatic)
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1567
                throw new IllegalStateException("Automatic modules cannot declare"
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1568
                                                + " dependences");
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1569
            String mn = req.name();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1570
            if (name.equals(mn))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1571
                throw new IllegalArgumentException("Dependence on self");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1572
            if (requires.containsKey(mn))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1573
                throw new IllegalStateException("Dependence upon " + mn
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1574
                                                + " already declared");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1575
            requires.put(mn, req);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1576
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1577
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1578
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1579
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1580
         * Adds a dependence on a module with the given (and possibly empty)
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1581
         * set of modifiers. The dependence includes the version of the
47866
39db80b32b69 8191632: Typos in comments due to duplicating words
igerasim
parents: 47216
diff changeset
  1582
         * module that was recorded at compile-time.
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1583
         *
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1584
         * @param  ms
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1585
         *         The set of modifiers
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1586
         * @param  mn
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1587
         *         The module name
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1588
         * @param  compiledVersion
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1589
         *         The version of the module recorded at compile-time
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1590
         *
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1591
         * @return This builder
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1592
         *
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1593
         * @throws IllegalArgumentException
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1594
         *         If the module name is {@code null}, is not a legal module
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1595
         *         name, or is equal to the module name that this builder
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1596
         *         was initialized to build
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1597
         * @throws IllegalStateException
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1598
         *         If the dependence on the module has already been declared
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1599
         *         or this builder is for an automatic module
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1600
         */
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1601
        public Builder requires(Set<Requires.Modifier> ms,
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1602
                                String mn,
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1603
                                Version compiledVersion) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1604
            Objects.requireNonNull(compiledVersion);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1605
            if (strict)
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1606
                mn = requireModuleName(mn);
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1607
            return requires(new Requires(ms, mn, compiledVersion, null));
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1608
        }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1609
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1610
        /* package */Builder requires(Set<Requires.Modifier> ms,
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1611
                                      String mn,
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1612
                                      String rawCompiledVersion) {
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1613
            Requires r;
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1614
            try {
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1615
                Version v = Version.parse(rawCompiledVersion);
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1616
                r = new Requires(ms, mn, v, null);
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1617
            } catch (IllegalArgumentException e) {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1618
                if (strict) throw e;
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1619
                r = new Requires(ms, mn, null, rawCompiledVersion);
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1620
            }
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1621
            return requires(r);
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1622
        }
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1623
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1624
        /**
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  1625
         * Adds a dependence on a module with the given (and possibly empty)
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1626
         * set of modifiers.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1627
         *
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1628
         * @param  ms
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1629
         *         The set of modifiers
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1630
         * @param  mn
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1631
         *         The module name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1632
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1633
         * @return This builder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1634
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1635
         * @throws IllegalArgumentException
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1636
         *         If the module name is {@code null}, is not a legal module
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1637
         *         name, or is equal to the module name that this builder
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1638
         *         was initialized to build
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1639
         * @throws IllegalStateException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1640
         *         If the dependence on the module has already been declared
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1641
         *         or this builder is for an automatic module
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1642
         */
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1643
        public Builder requires(Set<Requires.Modifier> ms, String mn) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1644
            if (strict)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1645
                mn = requireModuleName(mn);
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1646
            return requires(new Requires(ms, mn, null, null));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1647
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1648
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1649
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1650
         * Adds a dependence on a module with an empty set of modifiers.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1651
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1652
         * @param  mn
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1653
         *         The module name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1654
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1655
         * @return This builder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1656
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1657
         * @throws IllegalArgumentException
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1658
         *         If the module name is {@code null}, is not a legal module
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1659
         *         name, or is equal to the module name that this builder
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1660
         *         was initialized to build
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1661
         * @throws IllegalStateException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1662
         *         If the dependence on the module has already been declared
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1663
         *         or this builder is for an automatic module
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1664
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1665
        public Builder requires(String mn) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1666
            return requires(EnumSet.noneOf(Requires.Modifier.class), mn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1667
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1668
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1669
        /**
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1670
         * Adds an exported package.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1671
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1672
         * @param  e
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1673
         *         The export
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1674
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1675
         * @return This builder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1676
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1677
         * @throws IllegalStateException
47004
b7e72fc752c9 8186684: Fix broken links in java.base API docs
jjg
parents: 46096
diff changeset
  1678
         *         If the {@link Exports#source() package} is already declared as
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1679
         *         exported or this builder is for an automatic module
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1680
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1681
        public Builder exports(Exports e) {
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1682
            if (automatic) {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1683
                throw new IllegalStateException("Automatic modules cannot declare"
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1684
                                                 + " exported packages");
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1685
            }
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1686
            String source = e.source();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1687
            if (exports.containsKey(source)) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1688
                throw new IllegalStateException("Exported package " + source
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1689
                                                 + " already declared");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1690
            }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1691
            exports.put(source, e);
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1692
            packages.add(source);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1693
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1694
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1695
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1696
        /**
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1697
         * Adds an exported package with the given (and possibly empty) set of
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1698
         * modifiers. The package is exported to a set of target modules.
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1699
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1700
         * @param  ms
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1701
         *         The set of modifiers
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1702
         * @param  pn
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1703
         *         The package name
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1704
         * @param  targets
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1705
         *         The set of target modules names
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1706
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1707
         * @return This builder
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1708
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1709
         * @throws IllegalArgumentException
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1710
         *         If the package name is {@code null} or is not a legal
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1711
         *         package name, the set of target modules is empty, or the set
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1712
         *         of target modules contains a name that is not a legal module
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1713
         *         name
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1714
         * @throws IllegalStateException
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1715
         *         If the package is already declared as exported
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1716
         *         or this builder is for an automatic module
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1717
         */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1718
        public Builder exports(Set<Exports.Modifier> ms,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1719
                               String pn,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1720
                               Set<String> targets)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1721
        {
51327
a19fda433921 8209003: Consolidate use of empty collections in java.lang.module
redestad
parents: 49529
diff changeset
  1722
            targets = new HashSet<>(targets);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1723
            if (targets.isEmpty())
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1724
                throw new IllegalArgumentException("Empty target set");
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1725
            if (strict) {
51327
a19fda433921 8209003: Consolidate use of empty collections in java.lang.module
redestad
parents: 49529
diff changeset
  1726
                requirePackageName(pn);
49529
c0bdb1b1ab4f 8200127: Replace collection.stream().forEach() with collection.forEach()
martin
parents: 48431
diff changeset
  1727
                targets.forEach(Checks::requireModuleName);
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1728
            }
51327
a19fda433921 8209003: Consolidate use of empty collections in java.lang.module
redestad
parents: 49529
diff changeset
  1729
            Exports e = new Exports(ms, pn, targets);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1730
            return exports(e);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1731
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1732
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1733
        /**
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1734
         * Adds an exported package with the given (and possibly empty) set of
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1735
         * modifiers. The package is exported to all modules.
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1736
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1737
         * @param  ms
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1738
         *         The set of modifiers
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1739
         * @param  pn
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1740
         *         The package name
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1741
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1742
         * @return This builder
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1743
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1744
         * @throws IllegalArgumentException
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1745
         *         If the package name is {@code null} or is not a legal
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1746
         *         package name
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1747
         * @throws IllegalStateException
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1748
         *         If the package is already declared as exported
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1749
         *         or this builder is for an automatic module
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1750
         */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1751
        public Builder exports(Set<Exports.Modifier> ms, String pn) {
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1752
            if (strict) {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1753
                requirePackageName(pn);
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1754
            }
51327
a19fda433921 8209003: Consolidate use of empty collections in java.lang.module
redestad
parents: 49529
diff changeset
  1755
            Exports e = new Exports(ms, pn, Set.of());
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1756
            return exports(e);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1757
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1758
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1759
        /**
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1760
         * Adds an exported package. The package is exported to a set of target
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1761
         * modules.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1762
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1763
         * @param  pn
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1764
         *         The package name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1765
         * @param  targets
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1766
         *         The set of target modules names
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1767
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1768
         * @return This builder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1769
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1770
         * @throws IllegalArgumentException
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1771
         *         If the package name is {@code null} or is not a legal
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1772
         *         package name, the set of target modules is empty, or the set
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1773
         *         of target modules contains a name that is not a legal module
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1774
         *         name
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1775
         * @throws IllegalStateException
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1776
         *         If the package is already declared as exported
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1777
         *         or this builder is for an automatic module
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1778
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1779
        public Builder exports(String pn, Set<String> targets) {
51327
a19fda433921 8209003: Consolidate use of empty collections in java.lang.module
redestad
parents: 49529
diff changeset
  1780
            return exports(Set.of(), pn, targets);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1781
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1782
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1783
        /**
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1784
         * Adds an exported package. The package is exported to all modules.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1785
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1786
         * @param  pn
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1787
         *         The package name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1788
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1789
         * @return This builder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1790
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1791
         * @throws IllegalArgumentException
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1792
         *         If the package name is {@code null} or is not a legal
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1793
         *         package name
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1794
         * @throws IllegalStateException
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1795
         *         If the package is already declared as exported
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1796
         *         or this builder is for an automatic module
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1797
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1798
        public Builder exports(String pn) {
51327
a19fda433921 8209003: Consolidate use of empty collections in java.lang.module
redestad
parents: 49529
diff changeset
  1799
            return exports(Set.of(), pn);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1800
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1801
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1802
        /**
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1803
         * Adds an open package.
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1804
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1805
         * @param  obj
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1806
         *         The {@code Opens} object
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1807
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1808
         * @return This builder
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1809
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1810
         * @throws IllegalStateException
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1811
         *         If the package is already declared as open, or this is a
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1812
         *         builder for an open module or automatic module
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1813
         */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1814
        public Builder opens(Opens obj) {
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1815
            if (open || automatic) {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1816
                throw new IllegalStateException("Open or automatic modules cannot"
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1817
                                                + " declare open packages");
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1818
            }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1819
            String source = obj.source();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1820
            if (opens.containsKey(source)) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1821
                throw new IllegalStateException("Open package " + source
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1822
                                                + " already declared");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1823
            }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1824
            opens.put(source, obj);
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1825
            packages.add(source);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1826
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1827
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1828
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1829
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1830
        /**
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1831
         * Adds an open package with the given (and possibly empty) set of
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1832
         * modifiers. The package is open to a set of target modules.
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1833
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1834
         * @param  ms
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1835
         *         The set of modifiers
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1836
         * @param  pn
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1837
         *         The package name
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1838
         * @param  targets
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1839
         *         The set of target modules names
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1840
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1841
         * @return This builder
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1842
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1843
         * @throws IllegalArgumentException
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1844
         *         If the package name is {@code null} or is not a legal
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1845
         *         package name, the set of target modules is empty, or the set
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1846
         *         of target modules contains a name that is not a legal module
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1847
         *         name
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1848
         * @throws IllegalStateException
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1849
         *         If the package is already declared as open, or this is a
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1850
         *         builder for an open module or automatic module
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1851
         */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1852
        public Builder opens(Set<Opens.Modifier> ms,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1853
                             String pn,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1854
                             Set<String> targets)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1855
        {
51327
a19fda433921 8209003: Consolidate use of empty collections in java.lang.module
redestad
parents: 49529
diff changeset
  1856
            targets = new HashSet<>(targets);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1857
            if (targets.isEmpty())
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1858
                throw new IllegalArgumentException("Empty target set");
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1859
            if (strict) {
51327
a19fda433921 8209003: Consolidate use of empty collections in java.lang.module
redestad
parents: 49529
diff changeset
  1860
                requirePackageName(pn);
49529
c0bdb1b1ab4f 8200127: Replace collection.stream().forEach() with collection.forEach()
martin
parents: 48431
diff changeset
  1861
                targets.forEach(Checks::requireModuleName);
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1862
            }
51327
a19fda433921 8209003: Consolidate use of empty collections in java.lang.module
redestad
parents: 49529
diff changeset
  1863
            Opens opens = new Opens(ms, pn, targets);
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1864
            return opens(opens);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1865
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1866
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1867
        /**
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1868
         * Adds an open package with the given (and possibly empty) set of
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1869
         * modifiers. The package is open to all modules.
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1870
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1871
         * @param  ms
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1872
         *         The set of modifiers
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1873
         * @param  pn
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1874
         *         The package name
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1875
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1876
         * @return This builder
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1877
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1878
         * @throws IllegalArgumentException
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1879
         *         If the package name is {@code null} or is not a legal
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1880
         *         package name
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1881
         * @throws IllegalStateException
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1882
         *         If the package is already declared as open, or this is a
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1883
         *         builder for an open module or automatic module
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1884
         */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1885
        public Builder opens(Set<Opens.Modifier> ms, String pn) {
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1886
            if (strict) {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1887
                requirePackageName(pn);
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1888
            }
51327
a19fda433921 8209003: Consolidate use of empty collections in java.lang.module
redestad
parents: 49529
diff changeset
  1889
            Opens e = new Opens(ms, pn, Set.of());
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1890
            return opens(e);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1891
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1892
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1893
        /**
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1894
         * Adds an open package. The package is open to a set of target modules.
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1895
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1896
         * @param  pn
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1897
         *         The package name
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1898
         * @param  targets
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1899
         *         The set of target modules names
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1900
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1901
         * @return This builder
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1902
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1903
         * @throws IllegalArgumentException
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1904
         *         If the package name is {@code null} or is not a legal
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1905
         *         package name, the set of target modules is empty, or the set
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1906
         *         of target modules contains a name that is not a legal module
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1907
         *         name
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1908
         * @throws IllegalStateException
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1909
         *         If the package is already declared as open, or this is a
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1910
         *         builder for an open module or automatic module
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1911
         */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1912
        public Builder opens(String pn, Set<String> targets) {
51327
a19fda433921 8209003: Consolidate use of empty collections in java.lang.module
redestad
parents: 49529
diff changeset
  1913
            return opens(Set.of(), pn, targets);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1914
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1915
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1916
        /**
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1917
         * Adds an open package. The package is open to all modules.
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1918
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1919
         * @param  pn
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1920
         *         The package name
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1921
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1922
         * @return This builder
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1923
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1924
         * @throws IllegalArgumentException
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1925
         *         If the package name is {@code null} or is not a legal
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1926
         *         package name
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1927
         * @throws IllegalStateException
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1928
         *         If the package is already declared as open, or this is a
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1929
         *         builder for an open module or automatic module
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1930
         */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1931
        public Builder opens(String pn) {
51327
a19fda433921 8209003: Consolidate use of empty collections in java.lang.module
redestad
parents: 49529
diff changeset
  1932
            return opens(Set.of(), pn);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1933
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1934
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1935
        /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1936
         * Adds a service dependence.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1937
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1938
         * @param  service
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1939
         *         The service type
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1940
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1941
         * @return This builder
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1942
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1943
         * @throws IllegalArgumentException
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1944
         *         If the service type is {@code null} or not a qualified name of
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1945
         *         a class in a named package
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1946
         * @throws IllegalStateException
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1947
         *         If a dependency on the service type has already been declared
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  1948
         *         or this is a builder for an automatic module
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1949
         */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1950
        public Builder uses(String service) {
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1951
            if (automatic)
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1952
                throw new IllegalStateException("Automatic modules can not declare"
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1953
                                                + " service dependences");
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1954
            if (uses.contains(requireServiceTypeName(service)))
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1955
                throw new IllegalStateException("Dependence upon service "
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1956
                                                + service + " already declared");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1957
            uses.add(service);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1958
            return this;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1959
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1960
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1961
        /**
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1962
         * Provides a service with one or more implementations. The package for
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1963
         * each {@link Provides#providers provider} (or provider factory) is
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1964
         * added to the module if not already added.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1965
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1966
         * @param  p
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1967
         *         The provides
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1968
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1969
         * @return This builder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1970
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1971
         * @throws IllegalStateException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1972
         *         If the providers for the service type have already been
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1973
         *         declared
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1974
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1975
        public Builder provides(Provides p) {
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1976
            String service = p.service();
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1977
            if (provides.containsKey(service))
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1978
                throw new IllegalStateException("Providers of service "
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1979
                                                + service + " already declared");
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1980
            provides.put(service, p);
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1981
            p.providers().forEach(name -> packages.add(packageName(name)));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1982
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1983
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1984
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1985
        /**
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1986
         * Provides implementations of a service. The package for each provider
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1987
         * (or provider factory) is added to the module if not already added.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1988
         *
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1989
         * @param  service
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1990
         *         The service type
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1991
         * @param  providers
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1992
         *         The list of provider or provider factory class names
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1993
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1994
         * @return This builder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1995
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1996
         * @throws IllegalArgumentException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1997
         *         If the service type or any of the provider class names is
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1998
         *         {@code null} or not a qualified name of a class in a named
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  1999
         *         package, or the list of provider class names is empty
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2000
         * @throws IllegalStateException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2001
         *         If the providers for the service type have already been
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2002
         *         declared
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2003
         */
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2004
        public Builder provides(String service, List<String> providers) {
51327
a19fda433921 8209003: Consolidate use of empty collections in java.lang.module
redestad
parents: 49529
diff changeset
  2005
            providers = new ArrayList<>(providers);
a19fda433921 8209003: Consolidate use of empty collections in java.lang.module
redestad
parents: 49529
diff changeset
  2006
            if (providers.isEmpty())
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2007
                throw new IllegalArgumentException("Empty providers set");
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2008
            if (strict) {
51327
a19fda433921 8209003: Consolidate use of empty collections in java.lang.module
redestad
parents: 49529
diff changeset
  2009
                requireServiceTypeName(service);
a19fda433921 8209003: Consolidate use of empty collections in java.lang.module
redestad
parents: 49529
diff changeset
  2010
                providers.forEach(Checks::requireServiceProviderName);
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2011
            } else {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2012
                // Disallow service/providers in unnamed package
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2013
                String pn = packageName(service);
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2014
                if (pn.isEmpty()) {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2015
                    throw new IllegalArgumentException(service
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2016
                                                       + ": unnamed package");
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2017
                }
51327
a19fda433921 8209003: Consolidate use of empty collections in java.lang.module
redestad
parents: 49529
diff changeset
  2018
                for (String name : providers) {
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2019
                    pn = packageName(name);
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2020
                    if (pn.isEmpty()) {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2021
                        throw new IllegalArgumentException(name
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2022
                                                           + ": unnamed package");
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2023
                    }
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2024
                }
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2025
            }
51327
a19fda433921 8209003: Consolidate use of empty collections in java.lang.module
redestad
parents: 49529
diff changeset
  2026
            Provides p = new Provides(service, providers);
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2027
            return provides(p);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2028
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2029
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2030
        /**
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2031
         * Adds packages to the module. All packages in the set of package names
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2032
         * that are not in the module are added to module.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2033
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2034
         * @param  pns
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2035
         *         The (possibly empty) set of package names
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2036
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2037
         * @return This builder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2038
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2039
         * @throws IllegalArgumentException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2040
         *         If any of the package names is {@code null} or is not a
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2041
         *         legal package name
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2042
         */
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2043
        public Builder packages(Set<String> pns) {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2044
            if (strict) {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2045
                pns = new HashSet<>(pns);
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2046
                pns.forEach(Checks::requirePackageName);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2047
            }
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2048
            this.packages.addAll(pns);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2049
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2050
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2051
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2052
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2053
         * Sets the module version.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2054
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2055
         * @param  v
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2056
         *         The version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2057
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2058
         * @return This builder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2059
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2060
        public Builder version(Version v) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2061
            version = requireNonNull(v);
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  2062
            rawVersionString = null;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2063
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2064
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2065
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2066
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2067
         * Sets the module version.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2068
         *
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2069
         * @param  vs
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2070
         *         The version string to parse
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2071
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2072
         * @return This builder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2073
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2074
         * @throws IllegalArgumentException
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2075
         *         If {@code vs} is {@code null} or cannot be parsed as a
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2076
         *         version string
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2077
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2078
         * @see Version#parse(String)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2079
         */
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2080
        public Builder version(String vs) {
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  2081
            try {
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  2082
                version = Version.parse(vs);
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  2083
                rawVersionString = null;
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  2084
            } catch (IllegalArgumentException e) {
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  2085
                if (strict) throw e;
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  2086
                version = null;
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  2087
                rawVersionString = vs;
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2088
            }
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  2089
            return this;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2090
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2091
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2092
        /**
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2093
         * Sets the module main class. The package for the main class is added
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44359
diff changeset
  2094
         * to the module if not already added. In other words, this method is
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44359
diff changeset
  2095
         * equivalent to first invoking this builder's {@link #packages(Set)
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44359
diff changeset
  2096
         * packages} method to add the package name of the main class.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2097
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2098
         * @param  mc
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2099
         *         The module main class
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2100
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2101
         * @return This builder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2102
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2103
         * @throws IllegalArgumentException
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2104
         *         If {@code mainClass} is {@code null} or not a qualified
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2105
         *         name of a class in a named package
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2106
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2107
        public Builder mainClass(String mc) {
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2108
            String pn;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2109
            if (strict) {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2110
                mc = requireQualifiedClassName("main class name", mc);
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2111
                pn = packageName(mc);
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2112
                assert !pn.isEmpty();
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2113
            } else {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2114
                // Disallow main class in unnamed package
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2115
                pn = packageName(mc);
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2116
                if (pn.isEmpty()) {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2117
                    throw new IllegalArgumentException(mc + ": unnamed package");
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2118
                }
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2119
            }
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44359
diff changeset
  2120
            packages.add(pn);
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2121
            mainClass = mc;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2122
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2123
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2124
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2125
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2126
         * Builds and returns a {@code ModuleDescriptor} from its components.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2127
         *
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2128
         * <p> The module will require "{@code java.base}" even if the dependence
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2129
         * has not been declared (the exception is when building a module named
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2130
         * "{@code java.base}" as it cannot require itself). The dependence on
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2131
         * "{@code java.base}" will have the {@link
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2132
         * java.lang.module.ModuleDescriptor.Requires.Modifier#MANDATED MANDATED}
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2133
         * modifier if the dependence was not declared. </p>
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2134
         *
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2135
         * @return The module descriptor
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2136
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2137
        public ModuleDescriptor build() {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2138
            Set<Requires> requires = new HashSet<>(this.requires.values());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2139
            Set<Exports> exports = new HashSet<>(this.exports.values());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2140
            Set<Opens> opens = new HashSet<>(this.opens.values());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2141
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2142
            // add dependency on java.base
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2143
            if (strict
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2144
                    && !name.equals("java.base")
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2145
                    && !this.requires.containsKey("java.base")) {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2146
                requires.add(new Requires(Set.of(Requires.Modifier.MANDATED),
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2147
                                          "java.base",
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  2148
                                          null,
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2149
                                          null));
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2150
            }
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2151
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2152
            Set<Provides> provides = new HashSet<>(this.provides.values());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2153
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2154
            return new ModuleDescriptor(name,
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  2155
                                        version,
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  2156
                                        rawVersionString,
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2157
                                        modifiers,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2158
                                        requires,
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2159
                                        exports,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2160
                                        opens,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2161
                                        uses,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2162
                                        provides,
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  2163
                                        packages,
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  2164
                                        mainClass);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2165
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2166
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2167
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2168
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2169
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2170
     * Compares this module descriptor to another.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2171
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2172
     * <p> Two {@code ModuleDescriptor} objects are compared by comparing their
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2173
     * module names lexicographically. Where the module names are equal then the
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2174
     * module versions are compared. When comparing the module versions then a
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2175
     * module descriptor with a version is considered to succeed a module
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  2176
     * descriptor that does not have a version. If both versions are {@linkplain
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  2177
     * Version#parse(String) unparseable} then the {@linkplain #rawVersion()
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  2178
     * raw version strings} are compared lexicographically. Where the module names
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  2179
     * are equal and the versions are equal (or not present in both), then the
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  2180
     * set of modifiers are compared. Sets of modifiers are compared by comparing
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2181
     * a <em>binary value</em> computed for each set. If a modifier is present
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2182
     * in the set then the bit at the position of its ordinal is {@code 1}
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2183
     * in the binary value, otherwise {@code 0}. If the two set of modifiers
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2184
     * are also equal then the other components of the module descriptors are
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2185
     * compared in a manner that is consistent with {@code equals}. </p>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2186
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2187
     * @param  that
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2188
     *         The module descriptor to compare
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2189
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2190
     * @return A negative integer, zero, or a positive integer if this module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2191
     *         descriptor is less than, equal to, or greater than the given
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2192
     *         module descriptor
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2193
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2194
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2195
    public int compareTo(ModuleDescriptor that) {
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2196
        if (this == that) return 0;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2197
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2198
        int c = this.name().compareTo(that.name());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2199
        if (c != 0) return c;
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2200
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2201
        c = compare(this.version, that.version);
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2202
        if (c != 0) return c;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2203
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  2204
        c = compare(this.rawVersionString, that.rawVersionString);
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  2205
        if (c != 0) return c;
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  2206
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2207
        long v1 = modsValue(this.modifiers());
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2208
        long v2 = modsValue(that.modifiers());
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2209
        c = Long.compare(v1, v2);
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2210
        if (c != 0) return c;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2211
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2212
        c = compare(this.requires, that.requires);
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2213
        if (c != 0) return c;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2214
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2215
        c = compare(this.packages, that.packages);
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2216
        if (c != 0) return c;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2217
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2218
        c = compare(this.exports, that.exports);
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2219
        if (c != 0) return c;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2220
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2221
        c = compare(this.opens, that.opens);
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2222
        if (c != 0) return c;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2223
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2224
        c = compare(this.uses, that.uses);
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2225
        if (c != 0) return c;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2226
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2227
        c = compare(this.provides, that.provides);
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2228
        if (c != 0) return c;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2229
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2230
        c = compare(this.mainClass, that.mainClass);
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2231
        if (c != 0) return c;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2232
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2233
        return 0;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2234
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2235
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2236
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2237
     * Tests this module descriptor for equality with the given object.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2238
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2239
     * <p> If the given object is not a {@code ModuleDescriptor} then this
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2240
     * method returns {@code false}. Two module descriptors are equal if each
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2241
     * of their corresponding components is equal. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2242
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2243
     * <p> This method satisfies the general contract of the {@link
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2244
     * java.lang.Object#equals(Object) Object.equals} method. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2245
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2246
     * @param   ob
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2247
     *          the object to which this object is to be compared
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2248
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2249
     * @return  {@code true} if, and only if, the given object is a module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2250
     *          descriptor that is equal to this module descriptor
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2251
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2252
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2253
    public boolean equals(Object ob) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2254
        if (ob == this)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2255
            return true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2256
        if (!(ob instanceof ModuleDescriptor))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2257
            return false;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2258
        ModuleDescriptor that = (ModuleDescriptor)ob;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2259
        return (name.equals(that.name)
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2260
                && modifiers.equals(that.modifiers)
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2261
                && requires.equals(that.requires)
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2262
                && Objects.equals(packages, that.packages)
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2263
                && exports.equals(that.exports)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2264
                && opens.equals(that.opens)
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2265
                && uses.equals(that.uses)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2266
                && provides.equals(that.provides)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2267
                && Objects.equals(version, that.version)
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  2268
                && Objects.equals(rawVersionString, that.rawVersionString)
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  2269
                && Objects.equals(mainClass, that.mainClass));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2270
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2271
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2272
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2273
     * Computes a hash code for this module descriptor.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2274
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2275
     * <p> The hash code is based upon the components of the module descriptor,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2276
     * and satisfies the general contract of the {@link Object#hashCode
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2277
     * Object.hashCode} method. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2278
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2279
     * @return The hash-code value for this module descriptor
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2280
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2281
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2282
    public int hashCode() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2283
        int hc = hash;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2284
        if (hc == 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2285
            hc = name.hashCode();
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2286
            hc = hc * 43 + Objects.hashCode(modifiers);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2287
            hc = hc * 43 + requires.hashCode();
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2288
            hc = hc * 43 + Objects.hashCode(packages);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2289
            hc = hc * 43 + exports.hashCode();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2290
            hc = hc * 43 + opens.hashCode();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2291
            hc = hc * 43 + uses.hashCode();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2292
            hc = hc * 43 + provides.hashCode();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2293
            hc = hc * 43 + Objects.hashCode(version);
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  2294
            hc = hc * 43 + Objects.hashCode(rawVersionString);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2295
            hc = hc * 43 + Objects.hashCode(mainClass);
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  2296
            if (hc == 0)
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  2297
                hc = -1;
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  2298
            hash = hc;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2299
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2300
        return hc;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2301
    }
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2302
    private transient int hash;  // cached hash code
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2303
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2304
    /**
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2305
     * <p> Returns a string describing the module. </p>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2306
     *
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2307
     * @return A string describing the module
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2308
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2309
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2310
    public String toString() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2311
        StringBuilder sb = new StringBuilder();
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2312
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2313
        if (isOpen())
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2314
            sb.append("open ");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2315
        sb.append("module { name: ").append(toNameAndVersion());
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2316
        if (!requires.isEmpty())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2317
            sb.append(", ").append(requires);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2318
        if (!uses.isEmpty())
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  2319
            sb.append(", uses: ").append(uses);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2320
        if (!exports.isEmpty())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2321
            sb.append(", exports: ").append(exports);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2322
        if (!opens.isEmpty())
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2323
            sb.append(", opens: ").append(opens);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2324
        if (!provides.isEmpty()) {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2325
            sb.append(", provides: ").append(provides);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2326
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2327
        sb.append(" }");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2328
        return sb.toString();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2329
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2330
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2331
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2332
    /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2333
     * Instantiates a builder to build a module descriptor.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2334
     *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2335
     * @param  name
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2336
     *         The module name
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2337
     * @param  ms
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2338
     *         The set of module modifiers
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2339
     *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2340
     * @return A new builder
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2341
     *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2342
     * @throws IllegalArgumentException
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2343
     *         If the module name is {@code null} or is not a legal module
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2344
     *         name, or the set of modifiers contains {@link
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2345
     *         Modifier#AUTOMATIC AUTOMATIC} with other modifiers
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2346
     */
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2347
    public static Builder newModule(String name, Set<Modifier> ms) {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2348
        Set<Modifier> mods = new HashSet<>(ms);
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2349
        if (mods.contains(Modifier.AUTOMATIC) && mods.size() > 1)
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2350
            throw new IllegalArgumentException("AUTOMATIC cannot be used with"
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2351
                                               + " other modifiers");
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2352
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2353
        return new Builder(name, true, mods);
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2354
    }
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2355
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2356
    /**
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2357
     * Instantiates a builder to build a module descriptor for a <em>normal</em>
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2358
     * module. This method is equivalent to invoking {@link #newModule(String,Set)
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2359
     * newModule} with an empty set of {@link ModuleDescriptor.Modifier modifiers}.
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2360
     *
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2361
     * @param  name
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2362
     *         The module name
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2363
     *
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2364
     * @return A new builder
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2365
     *
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2366
     * @throws IllegalArgumentException
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2367
     *         If the module name is {@code null} or is not a legal module
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2368
     *         name
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2369
     */
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2370
    public static Builder newModule(String name) {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2371
        return new Builder(name, true, Set.of());
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2372
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2373
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2374
    /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2375
     * Instantiates a builder to build a module descriptor for an open module.
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2376
     * This method is equivalent to invoking {@link #newModule(String,Set)
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2377
     * newModule} with the {@link ModuleDescriptor.Modifier#OPEN OPEN} modifier.
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2378
     *
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2379
     * <p> The builder for an open module cannot be used to declare any open
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2380
     * packages. </p>
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2381
     *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2382
     * @param  name
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2383
     *         The module name
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2384
     *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2385
     * @return A new builder that builds an open module
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2386
     *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2387
     * @throws IllegalArgumentException
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2388
     *         If the module name is {@code null} or is not a legal module
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2389
     *         name
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2390
     */
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2391
    public static Builder newOpenModule(String name) {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2392
        return new Builder(name, true, Set.of(Modifier.OPEN));
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2393
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2394
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2395
    /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2396
     * Instantiates a builder to build a module descriptor for an automatic
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2397
     * module. This method is equivalent to invoking {@link #newModule(String,Set)
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2398
     * newModule} with the {@link ModuleDescriptor.Modifier#AUTOMATIC AUTOMATIC}
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2399
     * modifier.
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2400
     *
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2401
     * <p> The builder for an automatic module cannot be used to declare module
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2402
     * or service dependences. It also cannot be used to declare any exported
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2403
     * or open packages. </p>
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2404
     *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2405
     * @param  name
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2406
     *         The module name
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2407
     *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2408
     * @return A new builder that builds an automatic module
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2409
     *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2410
     * @throws IllegalArgumentException
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2411
     *         If the module name is {@code null} or is not a legal module
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2412
     *         name
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2413
     *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2414
     * @see ModuleFinder#of(Path[])
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2415
     */
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2416
    public static Builder newAutomaticModule(String name) {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2417
        return new Builder(name, true, Set.of(Modifier.AUTOMATIC));
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2418
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2419
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2420
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2421
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2422
     * Reads the binary form of a module declaration from an input stream
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2423
     * as a module descriptor.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2424
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2425
     * <p> If the descriptor encoded in the input stream does not indicate a
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2426
     * set of packages in the module then the {@code packageFinder} will be
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2427
     * invoked. The set of packages that the {@code packageFinder} returns
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2428
     * must include all the packages that the module exports, opens, as well
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2429
     * as the packages of the service implementations that the module provides,
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2430
     * and the package of the main class (if the module has a main class). If
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2431
     * the {@code packageFinder} throws an {@link UncheckedIOException} then
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2432
     * {@link IOException} cause will be re-thrown. </p>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2433
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2434
     * <p> If there are bytes following the module descriptor then it is
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2435
     * implementation specific as to whether those bytes are read, ignored,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2436
     * or reported as an {@code InvalidModuleDescriptorException}. If this
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2437
     * method fails with an {@code InvalidModuleDescriptorException} or {@code
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2438
     * IOException} then it may do so after some, but not all, bytes have
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2439
     * been read from the input stream. It is strongly recommended that the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2440
     * stream be promptly closed and discarded if an exception occurs. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2441
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2442
     * @apiNote The {@code packageFinder} parameter is for use when reading
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2443
     * module descriptors from legacy module-artifact formats that do not
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2444
     * record the set of packages in the descriptor itself.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2445
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2446
     * @param  in
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2447
     *         The input stream
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2448
     * @param  packageFinder
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2449
     *         A supplier that can produce the set of packages
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2450
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2451
     * @return The module descriptor
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2452
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2453
     * @throws InvalidModuleDescriptorException
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2454
     *         If an invalid module descriptor is detected or the set of
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2455
     *         packages returned by the {@code packageFinder} does not include
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2456
     *         all of the packages obtained from the module descriptor
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2457
     * @throws IOException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2458
     *         If an I/O error occurs reading from the input stream or {@code
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2459
     *         UncheckedIOException} is thrown by the package finder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2460
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2461
    public static ModuleDescriptor read(InputStream in,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2462
                                        Supplier<Set<String>> packageFinder)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2463
        throws IOException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2464
    {
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  2465
        return ModuleInfo.read(in, requireNonNull(packageFinder)).descriptor();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2466
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2467
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2468
    /**
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2469
     * Reads the binary form of a module declaration from an input stream as a
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2470
     * module descriptor. This method works exactly as specified by the 2-arg
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2471
     * {@link #read(InputStream,Supplier) read} method with the exception that
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2472
     * a packager finder is not used to find additional packages when the
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2473
     * module descriptor read from the stream does not indicate the set of
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2474
     * packages.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2475
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2476
     * @param  in
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2477
     *         The input stream
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2478
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2479
     * @return The module descriptor
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2480
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2481
     * @throws InvalidModuleDescriptorException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2482
     *         If an invalid module descriptor is detected
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2483
     * @throws IOException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2484
     *         If an I/O error occurs reading from the input stream
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2485
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2486
    public static ModuleDescriptor read(InputStream in) throws IOException {
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  2487
        return ModuleInfo.read(in, null).descriptor();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2488
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2489
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2490
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2491
     * Reads the binary form of a module declaration from a byte buffer
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2492
     * as a module descriptor.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2493
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2494
     * <p> If the descriptor encoded in the byte buffer does not indicate a
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2495
     * set of packages in the module then the {@code packageFinder} will be
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2496
     * invoked. The set of packages that the {@code packageFinder} returns
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2497
     * must include all the packages that the module exports, opens, as well
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2498
     * as the packages of the service implementations that the module provides,
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2499
     * and the package of the main class (if the module has a main class). If
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2500
     * the {@code packageFinder} throws an {@link UncheckedIOException} then
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2501
     * {@link IOException} cause will be re-thrown. </p>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2502
     *
48431
b2cd597479ea 8194644: Typo in ModuleDescriptor.read javadoc
alanb
parents: 48261
diff changeset
  2503
     * <p> The module descriptor is read from the buffer starting at index
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2504
     * {@code p}, where {@code p} is the buffer's {@link ByteBuffer#position()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2505
     * position} when this method is invoked. Upon return the buffer's position
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2506
     * will be equal to {@code p + n} where {@code n} is the number of bytes
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2507
     * read from the buffer. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2508
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2509
     * <p> If there are bytes following the module descriptor then it is
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2510
     * implementation specific as to whether those bytes are read, ignored,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2511
     * or reported as an {@code InvalidModuleDescriptorException}. If this
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2512
     * method fails with an {@code InvalidModuleDescriptorException} then it
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2513
     * may do so after some, but not all, bytes have been read. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2514
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2515
     * @apiNote The {@code packageFinder} parameter is for use when reading
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2516
     * module descriptors from legacy module-artifact formats that do not
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2517
     * record the set of packages in the descriptor itself.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2518
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2519
     * @param  bb
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2520
     *         The byte buffer
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2521
     * @param  packageFinder
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2522
     *         A supplier that can produce the set of packages
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2523
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2524
     * @return The module descriptor
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2525
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2526
     * @throws InvalidModuleDescriptorException
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2527
     *         If an invalid module descriptor is detected or the set of
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2528
     *         packages returned by the {@code packageFinder} does not include
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2529
     *         all of the packages obtained from the module descriptor
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2530
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2531
    public static ModuleDescriptor read(ByteBuffer bb,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2532
                                        Supplier<Set<String>> packageFinder)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2533
    {
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  2534
        return ModuleInfo.read(bb, requireNonNull(packageFinder)).descriptor();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2535
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2536
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2537
    /**
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2538
     * Reads the binary form of a module declaration from a byte buffer as a
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2539
     * module descriptor. This method works exactly as specified by the 2-arg
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2540
     * {@link #read(ByteBuffer,Supplier) read} method with the exception that a
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2541
     * packager finder is not used to find additional packages when the module
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2542
     * descriptor encoded in the buffer does not indicate the set of packages.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2543
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2544
     * @param  bb
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2545
     *         The byte buffer
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2546
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2547
     * @return The module descriptor
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2548
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2549
     * @throws InvalidModuleDescriptorException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2550
     *         If an invalid module descriptor is detected
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2551
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2552
    public static ModuleDescriptor read(ByteBuffer bb) {
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  2553
        return ModuleInfo.read(bb, null).descriptor();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2554
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2555
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2556
    private static String packageName(String cn) {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2557
        int index = cn.lastIndexOf('.');
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2558
        return (index == -1) ? "" : cn.substring(0, index);
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2559
    }
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2560
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2561
    /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2562
     * Returns a string containing the given set of modifiers and label.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2563
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2564
    private static <M> String toString(Set<M> mods, String what) {
48261
43edfde828ab 8193454: ModuleDescriptor.{Requires,Exports,Open} toString should use toLowerCase(Local.ROOT)
alanb
parents: 47866
diff changeset
  2565
        return (Stream.concat(mods.stream().map(e -> e.toString()
43edfde828ab 8193454: ModuleDescriptor.{Requires,Exports,Open} toString should use toLowerCase(Local.ROOT)
alanb
parents: 47866
diff changeset
  2566
                                                      .toLowerCase(Locale.ROOT)),
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2567
                              Stream.of(what)))
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2568
                .collect(Collectors.joining(" "));
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2569
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2570
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2571
    private static <T extends Object & Comparable<? super T>>
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2572
    int compare(T obj1, T obj2) {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2573
        if (obj1 != null) {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2574
            return (obj2 != null) ? obj1.compareTo(obj2) : 1;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2575
        } else {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2576
            return (obj2 == null) ? 0 : -1;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2577
        }
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2578
    }
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2579
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2580
    /**
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2581
     * Compares two sets of {@code Comparable} objects.
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2582
     */
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2583
    @SuppressWarnings("unchecked")
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2584
    private static <T extends Object & Comparable<? super T>>
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2585
    int compare(Set<T> s1, Set<T> s2) {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2586
        T[] a1 = (T[]) s1.toArray();
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2587
        T[] a2 = (T[]) s2.toArray();
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2588
        Arrays.sort(a1);
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2589
        Arrays.sort(a2);
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2590
        return Arrays.compare(a1, a2);
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2591
    }
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2592
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2593
    private static <E extends Enum<E>> long modsValue(Set<E> set) {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2594
        long value = 0;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2595
        for (Enum<E> e : set) {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2596
            value += 1 << e.ordinal();
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2597
        }
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2598
        return value;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2599
    }
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2600
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2601
    static {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2602
        /**
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  2603
         * Setup the shared secret to allow code in other packages access
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  2604
         * private package methods in java.lang.module.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2605
         */
52427
3c6aa484536c 8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents: 51327
diff changeset
  2606
        jdk.internal.access.SharedSecrets
3c6aa484536c 8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents: 51327
diff changeset
  2607
            .setJavaLangModuleAccess(new jdk.internal.access.JavaLangModuleAccess() {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2608
                @Override
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  2609
                public Builder newModuleBuilder(String mn,
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  2610
                                                boolean strict,
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2611
                                                Set<ModuleDescriptor.Modifier> modifiers) {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2612
                    return new Builder(mn, strict, modifiers);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2613
                }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2614
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2615
                @Override
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2616
                public Set<String> packages(ModuleDescriptor.Builder builder) {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2617
                    return builder.packages();
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2618
                }
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  2619
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2620
                @Override
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2621
                public void requires(ModuleDescriptor.Builder builder,
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2622
                                     Set<Requires.Modifier> ms,
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2623
                                     String mn,
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  2624
                                     String rawCompiledVersion) {
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
  2625
                    builder.requires(ms, mn, rawCompiledVersion);
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  2626
                }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  2627
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  2628
                @Override
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  2629
                public Requires newRequires(Set<Requires.Modifier> ms, String mn, Version v) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  2630
                    return new Requires(ms, mn, v, true);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2631
                }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2632
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2633
                @Override
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2634
                public Exports newExports(Set<Exports.Modifier> ms, String source) {
51327
a19fda433921 8209003: Consolidate use of empty collections in java.lang.module
redestad
parents: 49529
diff changeset
  2635
                    return new Exports(ms, source, Set.of(), true);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2636
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2637
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2638
                @Override
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2639
                public Exports newExports(Set<Exports.Modifier> ms,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2640
                                          String source,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2641
                                          Set<String> targets) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2642
                    return new Exports(ms, source, targets, true);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2643
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2644
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2645
                @Override
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2646
                public Opens newOpens(Set<Opens.Modifier> ms,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2647
                                      String source,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2648
                                      Set<String> targets) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2649
                    return new Opens(ms, source, targets, true);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2650
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2651
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2652
                @Override
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2653
                public Opens newOpens(Set<Opens.Modifier> ms, String source) {
51327
a19fda433921 8209003: Consolidate use of empty collections in java.lang.module
redestad
parents: 49529
diff changeset
  2654
                    return new Opens(ms, source, Set.of(), true);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2655
                }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2656
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2657
                @Override
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2658
                public Provides newProvides(String service, List<String> providers) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2659
                    return new Provides(service, providers, true);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2660
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2661
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2662
                @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2663
                public ModuleDescriptor newModuleDescriptor(String name,
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  2664
                                                            Version version,
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2665
                                                            Set<ModuleDescriptor.Modifier> modifiers,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2666
                                                            Set<Requires> requires,
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2667
                                                            Set<Exports> exports,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2668
                                                            Set<Opens> opens,
38888
dd584fbea6a2 8159334: ModuleDescriptor retains overlapping sets for all and concealed packages
redestad
parents: 37880
diff changeset
  2669
                                                            Set<String> uses,
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2670
                                                            Set<Provides> provides,
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  2671
                                                            Set<String> packages,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2672
                                                            String mainClass,
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2673
                                                            int hashCode) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2674
                    return new ModuleDescriptor(name,
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  2675
                                                version,
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2676
                                                modifiers,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2677
                                                requires,
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2678
                                                exports,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2679
                                                opens,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2680
                                                uses,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2681
                                                provides,
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
  2682
                                                packages,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2683
                                                mainClass,
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2684
                                                hashCode,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2685
                                                false);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2686
                }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2687
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2688
                @Override
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2689
                public Configuration resolveAndBind(ModuleFinder finder,
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2690
                                                    Collection<String> roots,
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
  2691
                                                    PrintStream traceOutput)
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  2692
                {
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45004
diff changeset
  2693
                    return Configuration.resolveAndBind(finder, roots, traceOutput);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45004
diff changeset
  2694
                }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45004
diff changeset
  2695
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45004
diff changeset
  2696
                @Override
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45004
diff changeset
  2697
                public Configuration newConfiguration(ModuleFinder finder,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45004
diff changeset
  2698
                                                      Map<String, Set<String>> graph) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 45004
diff changeset
  2699
                    return new Configuration(finder, graph);
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  2700
                }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2701
            });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2702
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2703
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2704
}