1 /* 2 * Copyright (C) 2007-2010 Jlio Vilmar Gesser. 3 * Copyright (C) 2011, 2013-2016 The JavaParser Team. 4 * 5 * This file is part of JavaParser. 6 * 7 * JavaParser can be used either under the terms of 8 * a) the GNU Lesser General Public License as published by 9 * the Free Software Foundation, either version 3 of the License, or 10 * (at your option) any later version. 11 * b) the terms of the Apache License 12 * 13 * You should have received a copy of both licenses in LICENCE.LGPL and 14 * LICENCE.APACHE. Please refer to those files for details. 15 * 16 * JavaParser is distributed in the hope that it will be useful, 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 * GNU Lesser General Public License for more details. 20 */ 21 package com.github.javaparser.ast.expr; 22 23 import com.github.javaparser.ast.AllFieldsConstructor; 24 import com.github.javaparser.ast.Node; 25 import com.github.javaparser.ast.visitor.CloneVisitor; 26 import com.github.javaparser.ast.visitor.GenericVisitor; 27 import com.github.javaparser.ast.visitor.VoidVisitor; 28 import com.github.javaparser.metamodel.JavaParserMetaModel; 29 import com.github.javaparser.metamodel.LongLiteralExprMetaModel; 30 import javax.annotation.Generated; 31 import com.github.javaparser.TokenRange; 32 import java.util.function.Consumer; 33 import java.util.Optional; 34 35 /** 36 * All ways to specify a long literal. 37 * <br/><code>8934l</code> 38 * <br/><code>0x01L</code> 39 * <br/><code>022l</code> 40 * <br/><code>0B10101010L</code> 41 * <br/><code>99999999L</code> 42 * 43 * @author Julio Vilmar Gesser 44 */ 45 public final class LongLiteralExpr extends LiteralStringValueExpr { 46 47 public LongLiteralExpr() { 48 this(null, "0"); 49 } 50 51 @AllFieldsConstructor 52 public LongLiteralExpr(final String value) { 53 this(null, value); 54 } 55 56 /** 57 * This constructor is used by the parser and is considered private. 58 */ 59 @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") 60 public LongLiteralExpr(TokenRange tokenRange, String value) { 61 super(tokenRange, value); 62 customInitialization(); 63 } 64 65 public LongLiteralExpr(final long value) { 66 this(null, String.valueOf(value)); 67 } 68 69 @Override 70 @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") 71 public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { 72 return v.visit(this, arg); 73 } 74 75 @Override 76 @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") 77 public <A> void accept(final VoidVisitor<A> v, final A arg) { 78 v.visit(this, arg); 79 } 80 81 @Override 82 @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") 83 public boolean remove(Node node) { 84 if (node == null) 85 return false; 86 return super.remove(node); 87 } 88 89 /** 90 * @return the literal value as an long while respecting different number representations 91 */ 92 public long asLong() { 93 String result = value.replaceAll("_", ""); 94 char lastChar = result.charAt(result.length() - 1); 95 if (lastChar == 'l' || lastChar == 'L') { 96 result = result.substring(0, result.length() - 1); 97 } 98 if (result.startsWith("0x") || result.startsWith("0X")) { 99 return Long.parseUnsignedLong(result.substring(2), 16); 100 } 101 if (result.startsWith("0b") || result.startsWith("0B")) { 102 return Long.parseUnsignedLong(result.substring(2), 2); 103 } 104 if (result.length() > 1 && result.startsWith("0")) { 105 return Long.parseUnsignedLong(result.substring(1), 8); 106 } 107 return Long.parseLong(result); 108 } 109 110 public LongLiteralExpr setLong(long value) { 111 this.value = String.valueOf(value); 112 return this; 113 } 114 115 @Override 116 @Generated("com.github.javaparser.generator.core.node.CloneGenerator") 117 public LongLiteralExpr clone() { 118 return (LongLiteralExpr) accept(new CloneVisitor(), null); 119 } 120 121 @Override 122 @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") 123 public LongLiteralExprMetaModel getMetaModel() { 124 return JavaParserMetaModel.longLiteralExprMetaModel; 125 } 126 127 @Override 128 @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") 129 public boolean replace(Node node, Node replacementNode) { 130 if (node == null) 131 return false; 132 return super.replace(node, replacementNode); 133 } 134 135 @Override 136 @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") 137 public boolean isLongLiteralExpr() { 138 return true; 139 } 140 141 @Override 142 @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") 143 public LongLiteralExpr asLongLiteralExpr() { 144 return this; 145 } 146 147 @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") 148 public void ifLongLiteralExpr(Consumer<LongLiteralExpr> action) { 149 action.accept(this); 150 } 151 152 @Override 153 @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") 154 public Optional<LongLiteralExpr> toLongLiteralExpr() { 155 return Optional.of(this); 156 } 157 } 158