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

Java tutorial

Introduction

Here is the source code for com.carmanconsulting.cassidy.pojo.assembly.step.CollectionStep.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 com.carmanconsulting.cassidy.util.CassidyUtils;
import me.prettyprint.cassandra.serializers.StringSerializer;
import org.apache.commons.lang3.Validate;

import java.util.Collection;
import java.util.List;

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

    private Class<? extends Collection> collectionType;

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

    public CollectionStep() {
    }

    public CollectionStep(Class<? extends Collection> collectionType) {
        this.collectionType = Validate.notNull(collectionType, "Collection type cannot be null.");
    }

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

    @Override
    @SuppressWarnings("unchecked")
    public void execute(AssemblyStack assemblyStack) {
        List<Object> elements = assemblyStack.drain();
        Collection<Object> collection = CassidyUtils.instantiate(collectionType);
        collection.addAll(elements);
        assemblyStack.push(collection);
    }

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

    @Override
    @SuppressWarnings("unchecked")
    public void initializeFromColumn(byte[] columnValue, CassidyContext context) {
        final String collectionTypeName = StringSerializer.get().fromBytes(columnValue);
        this.collectionType = context.classPath().resolveClass(collectionTypeName);
    }
}