/*
* 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.pojo.assembly.AssemblyStack;
import com.carmanconsulting.cassidy.pojo.assembly.AssemblyStep;
import com.carmanconsulting.cassidy.util.CassidyUtils;
import com.carmanconsulting.cassidy.util.ClassResolver;
import com.carmanconsulting.cassidy.util.ColumnValueDefinition;
import me.prettyprint.cassandra.serializers.DynamicCompositeSerializer;
import me.prettyprint.hector.api.Serializer;
import me.prettyprint.hector.api.beans.DynamicComposite;
import org.apache.commons.lang3.Validate;
import java.nio.ByteBuffer;
public class PushStep implements AssemblyStep {
//----------------------------------------------------------------------------------------------------------------------
// Fields
//----------------------------------------------------------------------------------------------------------------------
private Object value;
//----------------------------------------------------------------------------------------------------------------------
// Constructors
//----------------------------------------------------------------------------------------------------------------------
public PushStep() {
}
public PushStep(Object value) {
this.value = Validate.notNull(value, "Value may not be null.");
}
//----------------------------------------------------------------------------------------------------------------------
// AssemblyStep Implementation
//----------------------------------------------------------------------------------------------------------------------
@Override
public void execute(AssemblyStack assemblyStack) {
assemblyStack.push(value);
}
@Override
@SuppressWarnings("unchecked")
public void initializeFromColumn(byte[] columnValue, ClassResolver classResolver) {
DynamicComposite dynamicComposite = DynamicCompositeSerializer.get().fromBytes(columnValue);
Serializer<Object> serializer = (Serializer<Object>)CassidyUtils.instantiate(classResolver.resolveClass(String.valueOf(dynamicComposite.get(0))));
this.value = serializer.fromByteBuffer((ByteBuffer) dynamicComposite.get(1));
}
@Override
public ColumnValueDefinition<?> toColumnDefinition() {
Serializer<Object> serializer = CassidyUtils.inferSerializer(value);
final DynamicComposite dynamicComposite = new DynamicComposite(serializer.getClass().getName(), serializer.toByteBuffer(value));
return new ColumnValueDefinition<>(dynamicComposite, DynamicCompositeSerializer.get());
}
}