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 
  26 package com.sun.codemodel.internal;
  27 
  28 // Currently only exports directive is needed in this model.
  29 /**
  30  * Represents a Java module directive.
  31  * For example {@code "exports foo.bar;"} or {@code "requires foo.baz;"}.
  32  * @author Tomas Kraus
  33  */
  34 public abstract class JModuleDirective {
  35 
  36     // Only ExportsDirective is implemented.
  37     /**
  38      * Module directive type. Child class implements {@code getType()} method which returns corresponding value.
  39      */
  40     public enum Type {
  41        /** Directive starting with {@code requires} keyword. */
  42        RequiresDirective,
  43        /** Directive starting with {@code exports} keyword. */
  44        ExportsDirective,
  45     }
  46 
  47     /** Name argument of module directive. */
  48     protected final String name;
  49 
  50     /**
  51      * Creates an instance of Java module directive.
  52      * @param name name argument of module directive.
  53      * @throws IllegalArgumentException if the name argument is {@code null}.
  54      */
  55     JModuleDirective(final String name) {
  56         if (name == null) {
  57             throw new IllegalArgumentException("JModuleDirective name argument is null");
  58         }
  59         this.name = name;
  60     }
  61 
  62     /**
  63      * Gets the type of this module directive.
  64      * @return type of this module directive. Will never be {@code null}.
  65      */
  66     public abstract Type getType();
  67 
  68     /**
  69      * Print source code of this module directive.
  70      * @param f Java code formatter.
  71      * @return provided instance of Java code formatter.
  72      */
  73     public abstract JFormatter generate(final JFormatter f);
  74 
  75     /**
  76      * Compares this module directive to the specified object.
  77      * @param other The object to compare this {@link JModuleDirective} against.
  78      * @return {@code true} if the argument is not {@code null}
  79      *         and is a {@link JModuleDirective} object with the same type
  80      *         and equal name.
  81      */
  82     @Override
  83     public boolean equals(final Object other) {
  84         if (this == other) {
  85             return true;
  86         }
  87         if (other instanceof JModuleDirective) {
  88             final JModuleDirective otherDirective = (JModuleDirective)other;
  89             return this.getType() == otherDirective.getType() && this.name.equals(otherDirective.name);
  90         }
  91         return false;
  92     }
  93 
  94 
  95     /**
  96      * Returns a hash code for this module directive based on directive type and name.
  97      * The hash code for a module directive is computed as <blockquote><pre>
  98      * 97 * Integer.hashCode(type_ordinal_value + 1) + name.hashCode()
  99      * </pre></blockquote> using {@code int} arithmetic.
 100      * @return a hash code value for this object.
 101      */
 102     @Override
 103     public int hashCode() {
 104         return 97 * (Integer.hashCode(getType().ordinal() + 1)) + name.hashCode();
 105     }
 106 
 107     /**
 108      * Gets the name of this module directive.
 109      * @return name of this module directive.
 110      */
 111     public String name() {
 112         return name;
 113     }
 114 
 115 }