Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed 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 com.facebook.presto.type;
 
 
 import java.util.List;
 
 import static com.facebook.presto.type.TypeJsonUtils.createBlock;
 import static com.facebook.presto.type.TypeJsonUtils.getObjectList;
 import static com.facebook.presto.type.TypeJsonUtils.stackRepresentationToObject;
 import static com.facebook.presto.type.TypeUtils.parameterizedTypeName;
 import static com.google.common.base.Preconditions.checkNotNull;
 
 public class ArrayType
         extends AbstractVariableWidthType
 {
     private static final ObjectMapper OBJECT_MAPPER = new ObjectMapperProvider().get().registerModule(new SimpleModule().addSerializer(Slice.classnew SliceSerializer()));
 
     private final Type elementType;
 
     public ArrayType(Type elementType)
     {
         super(parameterizedTypeName("array"elementType.getTypeSignature()), Slice.class);
         this. = checkNotNull(elementType"elementType is null");
     }
 
     public Type getElementType()
     {
         return ;
     }

    
Takes a list of stack types and converts them to the stack representation of an array
 
     public static Slice toStackRepresentation(List<?> values)
     {
         try {
             return Slices.utf8Slice(.writeValueAsString(values));
         }
         catch (JsonProcessingException e) {
             throw Throwables.propagate(e);
         }
     }

    
Takes a list of json encoded slices and converts them to the stack representation of an array
 
     public static Slice rawSlicesToStackRepresentation(List<Slicevalues)
     {
         try {
             return Slices.utf8Slice(.writeValueAsString(values));
         }
         catch (JsonProcessingException e) {
             throw Throwables.propagate(e);
         }
     }
 
     @Override
     public boolean isComparable()
     {
         return .isComparable();
     }
 
     @Override
     public boolean isOrderable()
     {
         return .isOrderable();
    }
    @Override
    public boolean equalTo(Block leftBlockint leftPositionBlock rightBlockint rightPosition)
    {
        return compareTo(leftBlockleftPositionrightBlockrightPosition) == 0;
    }
    @Override
    public int hash(Block blockint position)
    {
        Slice value = getSlice(blockposition);
        List<Objectarray = getObjectList(value);
        List<IntegerhashArray = new ArrayList<Integer>();
        for (Object element : array) {
            checkElementNotNull(element);
            hashArray.add(.hash(createBlock(element), 0));
        }
        return Objects.hash(hashArray);
    }
    @Override
    public int compareTo(Block leftBlockint leftPositionBlock rightBlockint rightPosition)
    {
        Slice leftSlice = getSlice(leftBlockleftPosition);
        Slice rightSlice = getSlice(rightBlockrightPosition);
        List<ObjectleftArray = getObjectList(leftSlice);
        List<ObjectrightArray = getObjectList(rightSlice);
        int len = Math.min(leftArray.size(), rightArray.size());
        int index = 0;
        while (index < len) {
            checkElementNotNull(leftArray.get(index));
            checkElementNotNull(rightArray.get(index));
            int comparison = .compareTo(createBlock(leftArray.get(index)), 0,
                    createBlock(rightArray.get(index)), 0);
            if (comparison != 0) {
                return comparison;
            }
            index++;
        }
        if (index == len) {
            return leftArray.size() - rightArray.size();
        }
        return 0;
    }
    private static void checkElementNotNull(Object element)
    {
        if (element == null) {
            throw new PrestoException(."ARRAY comparison not supported for arrays with null elements");
        }
    }
    @Override
    public Object getObjectValue(ConnectorSession sessionBlock blockint position)
    {
        if (block.isNull(position)) {
            return null;
        }
        Slice slice = block.getSlice(position, 0, block.getLength(position));
        return stackRepresentationToObject(sessionslicethis);
    }
    @Override
    public void appendTo(Block blockint positionBlockBuilder blockBuilder)
    {
        if (block.isNull(position)) {
            blockBuilder.appendNull();
        }
        else {
            block.writeBytesTo(position, 0, block.getLength(position), blockBuilder);
            blockBuilder.closeEntry();
        }
    }
    @Override
    public Slice getSlice(Block blockint position)
    {
        return block.getSlice(position, 0, block.getLength(position));
    }
    @Override
    public void writeSlice(BlockBuilder blockBuilderSlice value)
    {
        writeSlice(blockBuildervalue, 0, value.length());
    }
    @Override
    public void writeSlice(BlockBuilder blockBuilderSlice valueint offsetint length)
    {
        blockBuilder.writeBytes(valueoffsetlength).closeEntry();
    }
    @Override
    public BlockBuilder createBlockBuilder(BlockBuilderStatus blockBuilderStatus)
    {
        return new VariableWidthBlockBuilder(blockBuilderStatus);
    }
    @Override
    public List<TypegetTypeParameters()
    {
        return ImmutableList.of(getElementType());
    }
New to GrepCode? Check out our FAQ X