shahrs87 commented on a change in pull request #2707: URL: https://github.com/apache/hbase/pull/2707#discussion_r537234287
########## File path: hbase-common/src/main/java/org/apache/hadoop/hbase/TagBuilderFactory.java ########## @@ -0,0 +1,89 @@ +/** + * Copyright The Apache Software Foundation + * + * 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 + * + * http://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.hadoop.hbase; + +import java.nio.ByteBuffer; +import org.apache.yetus.audience.InterfaceAudience; + +/** + * Factory to create Tags. + */ +@InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.COPROC) +public final class TagBuilderFactory { + + public static TagBuilder create() { + return new TagBuilderImpl(); + } +} + +/** + * Builder implementation to create either {@link ArrayBackedTag} or {@link ByteBufferTag}<br> + * Call setTagValue(byte[]) method if you want to create {@link ArrayBackedTag} or + * call setTagValue(ByteBuffer) if you want to create {@link ByteBufferTag} + */ +class TagBuilderImpl implements TagBuilder { + // This assumes that we never create tag with value less than 0. + private byte tagType = (byte)-1; + private byte[] tagBytes = null; + private ByteBuffer tagBuffer = null; + public static final String TAG_TYPE_NOT_SET_EXCEPTION = "Need to set type of the tag."; + public static final String TAG_VALUE_NULL_EXCEPTION = + "Both tagBytes and tagBuffer can't be null"; + + @Override + public TagBuilder setTagType(byte tagType) { + this.tagType = tagType; + return this; + } + + @Override + public TagBuilder setTagValue(byte[] tagBytes) { + this.tagBytes = tagBytes; + this.tagBuffer = null; + return this; + } + + @Override + public TagBuilder setTagValue(ByteBuffer value) { + this.tagBuffer = value; + this.tagBytes = null; + return this; + } + + private void validate() { + if (tagType == -1) { + throw new IllegalArgumentException(TAG_TYPE_NOT_SET_EXCEPTION); + } + if (tagBytes == null && tagBuffer == null) { + throw new IllegalArgumentException(TAG_VALUE_NULL_EXCEPTION); + } + } + + @Override + public Tag build() { + validate(); + if (tagBytes != null) { + return new ArrayBackedTag(tagType, tagBytes); + } else { + return new ByteBufferTag(tagType, tagBuffer, -Tag.INFRASTRUCTURE_SIZE, Review comment: When I was writing this code, I also felt its bit hacky and why would we ever want to create BB backed tag but since ByteBuffer backed tag implementation already exists, I thought to provide an implementation. Maybe if we agree just to provide Array backed Tag, then I can remove BB backed tag also. ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: us...@infra.apache.org