1/*
2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.codemodel.internal;
27
28/**
29 * Represents a Java module {@code requires} directive.
30 * For example {@code "requires foo.bar;"} or {@code "requires public foo.baz;"}.
31 * @author Tomas Kraus
32 */
33public class JRequiresDirective extends JModuleDirective {
34
35    /** Public readability modifier. */
36    final boolean isPublic;
37
38    /** Static modifier. */
39    final boolean isStatic;
40
41    /**
42     * Creates an instance of Java module {@code requires} directive.
43     * @param name name of required module or service.
44     * @param isPublic Use {@code public} modifier.
45     * @param isStatic Use {@code static} modifier.
46     * @throws IllegalArgumentException if the name argument is {@code null}.
47     */
48    JRequiresDirective(final String name, final boolean isPublic, final boolean isStatic) {
49        super(name);
50        this.isPublic = isPublic;
51        this.isStatic = isStatic;
52    }
53
54    /**
55     * Gets the type of this module directive.
56     * @return type of this module directive. Will always return {@code Type.RequiresDirective}.
57     */
58    @Override
59    public Type getType() {
60        return Type.RequiresDirective;
61    }
62
63    /**
64     * Print source code of {@code requires} module directive modifiers:
65     * {@code public} and {@code static} keywords for module dependency.
66     * @param f Java code formatter.
67     */
68    protected void generateModifiers(final JFormatter f) {
69        if (isPublic) {
70            f.p("public");
71        }
72        if (isStatic) {
73            f.p("static");
74        }
75    }
76
77    /**
78     * Print source code of this module directive.
79     * @param f Java code formatter.
80     * @return provided instance of Java code formatter.
81     */
82    @Override
83    public JFormatter generate(final JFormatter f) {
84        f.p("requires");
85        generateModifiers(f);
86        f.p(name);
87        f.p(';').nl();
88        return f;
89    }
90
91}
92