View Javadoc
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-2019 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 javax.annotation.Nonnull;
44  
45  /**
46   * A Java expression.
47   * <p>
48   * Unlike most of CodeModel, JExpressions are built bottom-up ( meaning you
49   * start from leaves and then gradually build complicated expressions by
50   * combining them.)
51   * <p>
52   * {@link IJExpression} defines a series of composer methods, which returns a
53   * complicated expression (by often taking other {@link IJExpression}s as
54   * parameters. For example, you can build "5+2" by
55   * <tt>JExpr.lit(5).add(JExpr.lit(2))</tt>
56   */
57  public interface IJExpression extends IJGenerable
58  {
59    /**
60     * @return <code>-[this]" from "[this]</code>.
61     */
62    @Nonnull
63    default IJExpression minus ()
64    {
65      return JOp.minus (this);
66    }
67  
68    /**
69     * Logical 'not' <tt>'!x'</tt>.
70     *
71     * @return <code>![this]" from "[this]</code>.
72     */
73    @Nonnull
74    default IJExpression not ()
75    {
76      return JOp.not (this);
77    }
78  
79    /**
80     * @return <code>~[this]" from "[this]</code>.
81     */
82    @Nonnull
83    default IJExpression complement ()
84    {
85      return JOp.complement (this);
86    }
87  
88    /**
89     * @return <code>[this]++" from "[this]</code>.
90     */
91    @Nonnull
92    default IJExpression incr ()
93    {
94      return postincr ();
95    }
96  
97    /**
98     * @return <code>[this]++" from "[this]</code>.
99     */
100   @Nonnull
101   default IJExpression postincr ()
102   {
103     return JOp.postincr (this);
104   }
105 
106   /**
107    * @return <code>++[this]" from "[this]</code>.
108    */
109   @Nonnull
110   default IJExpression preincr ()
111   {
112     return JOp.preincr (this);
113   }
114 
115   /**
116    * @return <code>[this]--" from "[this]</code>.
117    */
118   @Nonnull
119   default IJExpression decr ()
120   {
121     return postdecr ();
122   }
123 
124   /**
125    * @return <code>[this]--" from "[this]</code>.
126    */
127   @Nonnull
128   default IJExpression postdecr ()
129   {
130     return JOp.postdecr (this);
131   }
132 
133   /**
134    * @return <code>--[this]" from "[this]</code>.
135    */
136   @Nonnull
137   default IJExpression predecr ()
138   {
139     return JOp.predecr (this);
140   }
141 
142   /**
143    * @param rhs
144    *        value to add
145    * @return <code>[this]+[rhs]</code>.
146    */
147   @Nonnull
148   default IJExpressionression">IJExpression plus (@Nonnull final IJExpression rhs)
149   {
150     return JOp.plus (this, rhs);
151   }
152 
153   /**
154    * @param rhs
155    *        value to add
156    * @return <code>[this]+[rhs]</code>.
157    */
158   @Nonnull
159   default IJExpression plus (final double rhs)
160   {
161     return plus (JExpr.lit (rhs));
162   }
163 
164   /**
165    * @param rhs
166    *        value to add
167    * @return <code>[this]+[rhs]</code>.
168    */
169   @Nonnull
170   default IJExpression plus (final float rhs)
171   {
172     return plus (JExpr.lit (rhs));
173   }
174 
175   /**
176    * @param rhs
177    *        value to add
178    * @return <code>[this]+[rhs]</code>.
179    */
180   @Nonnull
181   default IJExpression plus (final int rhs)
182   {
183     return plus (JExpr.lit (rhs));
184   }
185 
186   /**
187    * @param rhs
188    *        value to add
189    * @return <code>[this]+[rhs]</code>.
190    */
191   @Nonnull
192   default IJExpression plus (final long rhs)
193   {
194     return plus (JExpr.lit (rhs));
195   }
196 
197   /**
198    * @param rhs
199    *        value to add
200    * @return <code>[this]+[rhs]</code>.
201    */
202   @Nonnull
203   default IJExpression plus (@Nonnull final String rhs)
204   {
205     return plus (JExpr.lit (rhs));
206   }
207 
208   /**
209    * @param rhs
210    *        value to add
211    * @return <code>[this]-[rhs]</code>.
212    */
213   @Nonnull
214   default IJExpressionession">IJExpression minus (@Nonnull final IJExpression rhs)
215   {
216     return JOp.minus (this, rhs);
217   }
218 
219   /**
220    * @param rhs
221    *        value to subtract
222    * @return <code>[this]-[rhs]</code>.
223    */
224   @Nonnull
225   default IJExpression minus (final double rhs)
226   {
227     return minus (JExpr.lit (rhs));
228   }
229 
230   /**
231    * @param rhs
232    *        value to subtract
233    * @return <code>[this]-[rhs]</code>.
234    */
235   @Nonnull
236   default IJExpression minus (final float rhs)
237   {
238     return minus (JExpr.lit (rhs));
239   }
240 
241   /**
242    * @param rhs
243    *        value to subtract
244    * @return <code>[this]-[rhs]</code>.
245    */
246   @Nonnull
247   default IJExpression minus (final int rhs)
248   {
249     return minus (JExpr.lit (rhs));
250   }
251 
252   /**
253    * @param rhs
254    *        value to subtract
255    * @return <code>[this]-[rhs]</code>.
256    */
257   @Nonnull
258   default IJExpression minus (final long rhs)
259   {
260     return minus (JExpr.lit (rhs));
261   }
262 
263   /**
264    * @param rhs
265    *        value to multiply
266    * @return <code>[this]*[rhs]</code>.
267    */
268   @Nonnull
269   default IJExpressionpression">IJExpression mul (@Nonnull final IJExpression rhs)
270   {
271     return JOp.mul (this, rhs);
272   }
273 
274   /**
275    * @param rhs
276    *        value to multiply
277    * @return <code>[this]*[rhs]</code>.
278    */
279   @Nonnull
280   default IJExpression mul (final double rhs)
281   {
282     return mul (JExpr.lit (rhs));
283   }
284 
285   /**
286    * @param rhs
287    *        value to multiply
288    * @return <code>[this]*[rhs]</code>.
289    */
290   @Nonnull
291   default IJExpression mul (final float rhs)
292   {
293     return mul (JExpr.lit (rhs));
294   }
295 
296   /**
297    * @param rhs
298    *        value to multiply
299    * @return <code>[this]*[rhs]</code>.
300    */
301   @Nonnull
302   default IJExpression mul (final int rhs)
303   {
304     return mul (JExpr.lit (rhs));
305   }
306 
307   /**
308    * @param rhs
309    *        value to multiply
310    * @return <code>[this]*[rhs]</code>.
311    */
312   @Nonnull
313   default IJExpression mul (final long rhs)
314   {
315     return mul (JExpr.lit (rhs));
316   }
317 
318   /**
319    * @param rhs
320    *        value to divide through
321    * @return <code>[this]/[rhs]</code>.
322    */
323   @Nonnull
324   default IJExpressionpression">IJExpression div (@Nonnull final IJExpression rhs)
325   {
326     return JOp.div (this, rhs);
327   }
328 
329   /**
330    * @param rhs
331    *        value to divide through
332    * @return <code>[this]/[rhs]</code>.
333    */
334   @Nonnull
335   default IJExpression div (final double rhs)
336   {
337     return div (JExpr.lit (rhs));
338   }
339 
340   /**
341    * @param rhs
342    *        value to divide through
343    * @return <code>[this]/[rhs]</code>.
344    */
345   @Nonnull
346   default IJExpression div (final float rhs)
347   {
348     return div (JExpr.lit (rhs));
349   }
350 
351   /**
352    * @param rhs
353    *        value to divide through
354    * @return <code>[this]/[rhs]</code>.
355    */
356   @Nonnull
357   default IJExpression div (final int rhs)
358   {
359     return div (JExpr.lit (rhs));
360   }
361 
362   /**
363    * @param rhs
364    *        value to divide through
365    * @return <code>[this]/[rhs]</code>.
366    */
367   @Nonnull
368   default IJExpression div (final long rhs)
369   {
370     return div (JExpr.lit (rhs));
371   }
372 
373   /**
374    * @param rhs
375    *        value to mod with
376    * @return <code>[this]%[rhs]</code>.
377    */
378   @Nonnull
379   default IJExpressionpression">IJExpression mod (@Nonnull final IJExpression rhs)
380   {
381     return JOp.mod (this, rhs);
382   }
383 
384   /**
385    * @param rhs
386    *        value to mod with
387    * @return <code>[this]%[rhs]</code>.
388    */
389   @Nonnull
390   default IJExpression mod (final int rhs)
391   {
392     return mod (JExpr.lit (rhs));
393   }
394 
395   /**
396    * @param rhs
397    *        value to mod with
398    * @return <code>[this]%[rhs]</code>.
399    */
400   @Nonnull
401   default IJExpression mod (final long rhs)
402   {
403     return mod (JExpr.lit (rhs));
404   }
405 
406   /**
407    * @param rhs
408    *        number of bits to shift
409    * @return <code>[this]&lt;&lt;[rhs]</code>.
410    */
411   @Nonnull
412   default IJExpressionpression">IJExpression shl (@Nonnull final IJExpression rhs)
413   {
414     return JOp.shl (this, rhs);
415   }
416 
417   /**
418    * @param rhs
419    *        number of bits to shift
420    * @return <code>[this]&lt;&lt;[rhs]</code>.
421    */
422   @Nonnull
423   default IJExpression shl (final int rhs)
424   {
425     return shl (JExpr.lit (rhs));
426   }
427 
428   /**
429    * @param rhs
430    *        number of bits to shift
431    * @return <code>[this] &gt;&gt; [rhs]</code>.
432    */
433   @Nonnull
434   default IJExpressionpression">IJExpression shr (@Nonnull final IJExpression rhs)
435   {
436     return JOp.shr (this, rhs);
437   }
438 
439   /**
440    * @param rhs
441    *        number of bits to shift
442    * @return <code>[this] &gt;&gt; [rhs]</code>.
443    */
444   @Nonnull
445   default IJExpression shr (final int rhs)
446   {
447     return shr (JExpr.lit (rhs));
448   }
449 
450   /**
451    * @param rhs
452    *        number of bits to shift
453    * @return <code>[this] &gt;&gt;&gt; [rhs]</code>.
454    */
455   @Nonnull
456   default IJExpressionression">IJExpression shrz (@Nonnull final IJExpression rhs)
457   {
458     return JOp.shrz (this, rhs);
459   }
460 
461   /**
462    * @param rhs
463    *        number of bits to shift
464    * @return <code>[this] &gt;&gt;&gt; [rhs]</code>.
465    */
466   @Nonnull
467   default IJExpression shrz (final int rhs)
468   {
469     return shrz (JExpr.lit (rhs));
470   }
471 
472   /**
473    * Bit-wise AND '&amp;'.
474    *
475    * @param rhs
476    *        value to combine with
477    * @return <code>[this] &amp; [rhs]</code>.
478    */
479   @Nonnull
480   default IJExpressionression">IJExpression band (@Nonnull final IJExpression rhs)
481   {
482     return JOp.band (this, rhs);
483   }
484 
485   /**
486    * Bit-wise OR '|'.
487    *
488    * @param rhs
489    *        value to combine with
490    * @return <code>[this] | [rhs]</code>.
491    */
492   @Nonnull
493   default IJExpressionpression">IJExpression bor (@Nonnull final IJExpression rhs)
494   {
495     return JOp.bor (this, rhs);
496   }
497 
498   /**
499    * Logical AND '&amp;&amp;'.
500    *
501    * @param rhs
502    *        value to combine with
503    * @return <code>[this] &amp;&amp; [rhs]</code>.
504    */
505   @Nonnull
506   default IJExpressionression">IJExpression cand (@Nonnull final IJExpression rhs)
507   {
508     return JOp.cand (this, rhs);
509   }
510 
511   /**
512    * Logical OR '||'.
513    *
514    * @param rhs
515    *        value to combine with
516    * @return <code>[this] || [rhs]</code>.
517    */
518   @Nonnull
519   default IJExpressionpression">IJExpression cor (@Nonnull final IJExpression rhs)
520   {
521     return JOp.cor (this, rhs);
522   }
523 
524   /**
525    * @param rhs
526    *        value to combine with
527    * @return <code>[this] ^ [rhs]</code>.
528    */
529   @Nonnull
530   default IJExpressionpression">IJExpression xor (@Nonnull final IJExpression rhs)
531   {
532     return JOp.xor (this, rhs);
533   }
534 
535   /**
536    * @param rhs
537    *        value to compare to
538    * @return <code>[this] &lt; [rhs]</code>.
539    */
540   @Nonnull
541   default IJExpressionxpression">IJExpression lt (@Nonnull final IJExpression rhs)
542   {
543     return JOp.lt (this, rhs);
544   }
545 
546   /**
547    * @param rhs
548    *        value to compare to
549    * @return <code>[this] &lt; [rhs]</code>.
550    */
551   @Nonnull
552   default IJExpression lt (final int rhs)
553   {
554     return lt (JExpr.lit (rhs));
555   }
556 
557   /**
558    * @return <code>[this] &lt; 0</code>.
559    */
560   @Nonnull
561   default IJExpression lt0 ()
562   {
563     return lt (0);
564   }
565 
566   /**
567    * @param rhs
568    *        value to compare to
569    * @return <code>[this] &lt;= [rhs]</code>.
570    */
571   @Nonnull
572   default IJExpressionpression">IJExpression lte (@Nonnull final IJExpression rhs)
573   {
574     return JOp.lte (this, rhs);
575   }
576 
577   /**
578    * @param rhs
579    *        value to compare to
580    * @return <code>[this] &lt;= [rhs]</code>.
581    */
582   @Nonnull
583   default IJExpression lte (final int rhs)
584   {
585     return lte (JExpr.lit (rhs));
586   }
587 
588   /**
589    * @return <code>[this] &lt;= 0</code>.
590    */
591   @Nonnull
592   default IJExpression lte0 ()
593   {
594     return lte (0);
595   }
596 
597   /**
598    * @param rhs
599    *        value to compare to
600    * @return <code>[this] &gt; [rhs]</code>.
601    */
602   @Nonnull
603   default IJExpressionxpression">IJExpression gt (@Nonnull final IJExpression rhs)
604   {
605     return JOp.gt (this, rhs);
606   }
607 
608   /**
609    * @param rhs
610    *        value to compare to
611    * @return <code>[this] &gt; [rhs]</code>.
612    */
613   @Nonnull
614   default IJExpression gt (final int rhs)
615   {
616     return gt (JExpr.lit (rhs));
617   }
618 
619   /**
620    * @return <code>[this] &gt; 0</code>.
621    */
622   @Nonnull
623   default IJExpression gt0 ()
624   {
625     return gt (0);
626   }
627 
628   /**
629    * @param rhs
630    *        value to compare to
631    * @return <code>[this] &gt;= [rhs]</code>.
632    */
633   @Nonnull
634   default IJExpressionpression">IJExpression gte (@Nonnull final IJExpression rhs)
635   {
636     return JOp.gte (this, rhs);
637   }
638 
639   /**
640    * @param rhs
641    *        value to compare to
642    * @return <code>[this] &gt;= [rhs]</code>.
643    */
644   @Nonnull
645   default IJExpression gte (final int rhs)
646   {
647     return gte (JExpr.lit (rhs));
648   }
649 
650   /**
651    * @return <code>[this] &gt;= 0</code>.
652    */
653   @Nonnull
654   default IJExpression gte0 ()
655   {
656     return gte (0);
657   }
658 
659   /**
660    * Equals
661    *
662    * @param rhs
663    *        expression to compare to
664    * @return <code><em>expr</em> == <em>rhs</em></code>
665    */
666   @Nonnull
667   default IJExpressionxpression">IJExpression eq (@Nonnull final IJExpression rhs)
668   {
669     return JOp.eq (this, rhs);
670   }
671 
672   /**
673    * Shortcut for <code>eq (JExpr._null ())</code>
674    *
675    * @return <code><em>expr</em> == null</code>
676    */
677   @Nonnull
678   default IJExpression eqNull ()
679   {
680     return eq (JExpr._null ());
681   }
682 
683   /**
684    * Shortcut for <code>eq (JExpr.lit (0))</code>
685    *
686    * @return <code><em>expr</em> == 0</code>
687    */
688   @Nonnull
689   default IJExpression eq0 ()
690   {
691     return eq (JExpr.lit (0));
692   }
693 
694   /**
695    * Not equals
696    *
697    * @param rhs
698    *        expression to compare to
699    * @return <code><em>expr</em> != <em>rhs</em></code>
700    */
701   @Nonnull
702   default IJExpressionxpression">IJExpression ne (@Nonnull final IJExpression rhs)
703   {
704     return JOp.ne (this, rhs);
705   }
706 
707   /**
708    * Shortcut for <code>ne (JExpr._null ())</code>
709    *
710    * @return Never <code><em>expr</em> != null</code>
711    */
712   @Nonnull
713   default IJExpression neNull ()
714   {
715     return ne (JExpr._null ());
716   }
717 
718   /**
719    * Shortcut for <code>ne (JExpr.lit (0))</code>
720    *
721    * @return Never <code><em>expr</em> != 0</code>
722    */
723   @Nonnull
724   default IJExpression ne0 ()
725   {
726     return ne (JExpr.lit (0));
727   }
728 
729   /**
730    * @param rhs
731    *        type to check
732    * @return <code>[this] instanceof [rhs]</code>.
733    */
734   @Nonnull
735   default IJExpression _instanceof (@Nonnull final AbstractJType rhs)
736   {
737     return JOp._instanceof (this, rhs);
738   }
739 
740   /**
741    * @param aMethod
742    *        Method to be invoked
743    * @return <code>[this].[method]</code>. Arguments shall be added to the
744    *         returned {@link JInvocation} object.
745    */
746   @Nonnull
747   default JInvocation invoke (@Nonnull final JMethod aMethod)
748   {
749     return JExpr.invoke (this, aMethod);
750   }
751 
752   /**
753    * @param sMethod
754    *        name of the method to invoke
755    * @return <code>[this].[method]</code>. Arguments shall be added to the
756    *         returned {@link JInvocation} object.
757    */
758   @Nonnull
759   default JInvocation invoke (@Nonnull final String sMethod)
760   {
761     return JExpr.invoke (this, sMethod);
762   }
763 
764   @Nonnull
765   default JFieldRef ref (@Nonnull final JVar aField)
766   {
767     return JExpr.ref (this, aField);
768   }
769 
770   @Nonnull
771   default JFieldRef ref (@Nonnull final String sField)
772   {
773     return JExpr.ref (this, sField);
774   }
775 
776   /**
777    * @param aIndex
778    *        array index
779    * @return <code>[this] [ [index] ]</code>
780    */
781   @Nonnull
782   default JArrayCompRef component (@Nonnull final IJExpression aIndex)
783   {
784     return JExpr.component (this, aIndex);
785   }
786 
787   /**
788    * @param nIndex
789    *        array index
790    * @return <code>[this] [ [index] ]</code>
791    */
792   @Nonnull
793   default JArrayCompRef component (final int nIndex)
794   {
795     return component (JExpr.lit (nIndex));
796   }
797 
798   /**
799    * @return <code>[this] [0]</code>
800    */
801   @Nonnull
802   default JArrayCompRef component0 ()
803   {
804     return component (0);
805   }
806 
807   @Nonnull
808   default JCast castTo (@Nonnull final AbstractJType aType)
809   {
810     return JExpr.cast (aType, this);
811   }
812 }