com.carmanconsulting.cassidy.pojo.assembly.step.ArrayStep.java Source code

Java tutorial

Introduction

Here is the source code for com.carmanconsulting.cassidy.pojo.assembly.step.ArrayStep.java

Source

/*
 * Copyright (c) 2014 Carman Consulting, Inc.
 *
 * 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.carmanconsulting.cassidy.pojo.assembly.step;

import com.carmanconsulting.cassidy.context.CassidyContext;
import com.carmanconsulting.cassidy.pojo.assembly.AssemblyStack;
import com.carmanconsulting.cassidy.pojo.assembly.AssemblyStep;
import me.prettyprint.cassandra.serializers.StringSerializer;
import org.apache.commons.lang3.Validate;

import java.lang.reflect.Array;
import java.util.List;

public class ArrayStep implements AssemblyStep {
    //----------------------------------------------------------------------------------------------------------------------
    // Fields
    //----------------------------------------------------------------------------------------------------------------------

    private Class<?> elementType;

    //----------------------------------------------------------------------------------------------------------------------
    // Constructors
    //----------------------------------------------------------------------------------------------------------------------

    public ArrayStep() {
    }

    public ArrayStep(Class<?> elementType) {
        this.elementType = Validate.notNull(elementType, "Element type cannot be null.");
    }

    //----------------------------------------------------------------------------------------------------------------------
    // AssemblyStep Implementation
    //----------------------------------------------------------------------------------------------------------------------

    @Override
    public void execute(AssemblyStack assemblyStack) {
        final List<Object> elements = assemblyStack.drain();
        Object array = Array.newInstance(elementType, elements.size());
        for (int i = 0; i < elements.size(); i++) {
            Object element = elements.get(i);
            Array.set(array, i, element);
        }
        assemblyStack.push(array);
    }

    @Override
    public Object getColumnValue() {
        return elementType.getName();
    }

    @Override
    public void initializeFromColumn(byte[] columnValue, CassidyContext context) {
        this.elementType = context.classPath().resolveClass(StringSerializer.get().fromBytes(columnValue));
    }
}