keith-turner commented on code in PR #83: URL: https://github.com/apache/accumulo-access/pull/83#discussion_r1939736817
########## src/main/java/org/apache/accumulo/access/ParsedAccessExpressionImpl.java: ########## @@ -0,0 +1,171 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.accumulo.access; + +import static java.nio.charset.StandardCharsets.UTF_8; +import static org.apache.accumulo.access.ByteUtils.AND_OPERATOR; +import static org.apache.accumulo.access.ByteUtils.OR_OPERATOR; +import static org.apache.accumulo.access.ByteUtils.isAndOrOperator; +import static org.apache.accumulo.access.ParsedAccessExpression.ExpressionType.AND; +import static org.apache.accumulo.access.ParsedAccessExpression.ExpressionType.AUTHORIZATION; +import static org.apache.accumulo.access.ParsedAccessExpression.ExpressionType.OR; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.atomic.AtomicReference; + +// This class is intentionally package private +final class ParsedAccessExpressionImpl extends ParsedAccessExpression { + + private static final long serialVersionUID = 1L; + + private final byte[] expression; + private final int offset; + private final int length; + + private final ExpressionType type; + private final List<ParsedAccessExpression> children; + + private final AtomicReference<String> stringExpression = new AtomicReference<>(null); + + static final ParsedAccessExpression EMPTY = new ParsedAccessExpressionImpl(); + + ParsedAccessExpressionImpl(byte operator, byte[] expression, int offset, int length, + List<ParsedAccessExpression> children) { + if (children.isEmpty()) { + throw new IllegalArgumentException("Must have children with an operator"); + } + + if (operator != AND_OPERATOR && operator != OR_OPERATOR) { + throw new IllegalArgumentException("Unknown operator " + operator); + } else if (operator == AND_OPERATOR) { + this.type = AND; + } else { + this.type = OR; + } + + this.expression = expression; + this.offset = offset; + this.length = length; + this.children = List.copyOf(children); + } + + ParsedAccessExpressionImpl(byte[] expression, int offset, int length) { + this.type = AUTHORIZATION; + this.expression = expression; + this.offset = offset; + this.length = length; + this.children = List.of(); + } + + ParsedAccessExpressionImpl() { + this.type = ExpressionType.EMPTY; + this.offset = 0; + this.length = 0; + this.expression = new byte[0]; + this.children = List.of(); + } + + @Override + public String getExpression() { + String strExp = stringExpression.get(); + if (strExp != null) { + return strExp; + } + strExp = new String(expression, offset, length, UTF_8); + stringExpression.compareAndSet(null, strExp); + return stringExpression.get(); + } + + @Override + public ExpressionType getType() { + return type; + } + + @Override + public List<ParsedAccessExpression> getChildren() { + return children; + } + + static ParsedAccessExpression parseExpression(byte[] expression) { + if (expression.length == 0) { + return ParsedAccessExpressionImpl.EMPTY; + } + + Tokenizer tokenizer = new Tokenizer(expression); + var parsed = ParsedAccessExpressionImpl.parseExpression(tokenizer, false); + + if (tokenizer.hasNext()) { + // not all input was read, so not a valid expression + tokenizer.error("Unexpected character '" + (char) tokenizer.peek() + "'"); + } + + return parsed; + } Review Comment: I was trying to have only public methods in the public API type to make it easier to see the public API when looking at the source code. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
