1 /**
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved.
5 * Portions Copyright 2013-2017 Philip Helger + contributors
6 *
7 * The contents of this file are subject to the terms of either the GNU
8 * General Public License Version 2 only ("GPL") or the Common Development
9 * and Distribution License("CDDL") (collectively, the "License"). You
10 * may not use this file except in compliance with the License. You can
11 * obtain a copy of the License at
12 * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
13 * or packager/legal/LICENSE.txt. See the License for the specific
14 * language governing permissions and limitations under the License.
15 *
16 * When distributing the software, include this License Header Notice in each
17 * file and include the License file at packager/legal/LICENSE.txt.
18 *
19 * GPL Classpath Exception:
20 * Oracle designates this particular file as subject to the "Classpath"
21 * exception as provided by Oracle in the GPL Version 2 section of the License
22 * file that accompanied this code.
23 *
24 * Modifications:
25 * If applicable, add the following below the License Header, with the fields
26 * enclosed by brackets [] replaced by your own identifying information:
27 * "Portions Copyright [year] [name of copyright owner]"
28 *
29 * Contributor(s):
30 * If you wish your version of this file to be governed by only the CDDL or
31 * only the GPL Version 2, indicate your decision by adding "[Contributor]
32 * elects to include this software in this distribution under the [CDDL or GPL
33 * Version 2] license." If you don't indicate a single choice of license, a
34 * recipient has the option to distribute your version of this file under
35 * either the CDDL, the GPL Version 2 or to extend the choice of license to
36 * its licensees as provided above. However, if you add GPL Version 2 code
37 * and therefore, elected the GPL Version 2 license, then the option applies
38 * only if the new code is made subject to such option by the copyright
39 * holder.
40 */
41 package com.helger.jcodemodel;
42
43 import static com.helger.jcodemodel.util.JCEqualsHelper.isEqual;
44 import static com.helger.jcodemodel.util.JCHashCodeGenerator.getHashCode;
45
46 import javax.annotation.Nonnull;
47
48 import com.helger.jcodemodel.util.JCValueEnforcer;
49
50 public class JOpUnary implements IJExpression
51 {
52 private final String m_sOperator;
53 private final IJExpression m_aExpr;
54 private final boolean m_bOperatorComesFirst;
55
56 /**
57 * Constructor for operator before expression
58 *
59 * @param sOperator
60 * operator
61 * @param aExpr
62 * expression
63 */
64 protected JOpUnary (@Nonnull final String sOperator, @Nonnull final IJExpression aExpr)
65 {
66 m_sOperator = JCValueEnforcer.notNull (sOperator, "Operator");
67 m_aExpr = JCValueEnforcer.notNull (aExpr, "Expression");
68 m_bOperatorComesFirst = true;
69 }
70
71 /**
72 * Constructor for expression before operator
73 *
74 * @param aExpr
75 * expression
76 * @param sOperator
77 * operator
78 */
79 protected JOpUnary (@Nonnull final IJExpression aExpr, @Nonnull final String sOperator)
80 {
81 m_sOperator = JCValueEnforcer.notNull (sOperator, "Operator");
82 m_aExpr = JCValueEnforcer.notNull (aExpr, "Expression");
83 m_bOperatorComesFirst = false;
84 }
85
86 @Nonnull
87 public String op ()
88 {
89 return m_sOperator;
90 }
91
92 @Nonnull
93 public IJExpression expr ()
94 {
95 return m_aExpr;
96 }
97
98 /**
99 * @return <code>true</code> if the operator comes first, <code>false</code>
100 * if the operator comes last
101 */
102 public boolean opFirst ()
103 {
104 return m_bOperatorComesFirst;
105 }
106
107 public void generate (@Nonnull final JFormatter f)
108 {
109 if (m_bOperatorComesFirst)
110 f.print ('(').print (m_sOperator).generable (m_aExpr).print (')');
111 else
112 f.print ('(').generable (m_aExpr).print (m_sOperator).print (')');
113 }
114
115 @Override
116 public boolean equals (final Object o)
117 {
118 if (o == this)
119 return true;
120 if (o == null || getClass () != o.getClass ())
121 return false;
122 final JOpUnary rhs = (JOpUnary) o;
123 return isEqual (m_sOperator, rhs.m_sOperator) &&
124 isEqual (m_aExpr, rhs.m_aExpr) &&
125 isEqual (m_bOperatorComesFirst, rhs.m_bOperatorComesFirst);
126 }
127
128 @Override
129 public int hashCode ()
130 {
131 return getHashCode (this, m_sOperator, m_aExpr, Boolean.valueOf (m_bOperatorComesFirst));
132 }
133 }