All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.oneandone.troilus.ColumnName Maven / Gradle / Ivy

There is a newer version: 0.18
Show newest version
/*
 * Copyright 1&1 Internet AG, https://github.com/1and1/
 * 
 * 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 net.oneandone.troilus;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.InetAddress;
import java.nio.ByteBuffer;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import net.oneandone.troilus.java7.Record;

import com.datastax.driver.core.TupleValue;
import com.google.common.collect.ImmutableList;


 
/**
 * A name which defines the class type of the associated value type 
 *
 * @param  the value type
 */
public abstract class ColumnName {
 
    private final String name;
    
    private ColumnName(String name) { 
        this.name = name;
    }
    
    
    /**
     * @return the name
     */
    public String getName() { 
        return name;
    }


    abstract T read(Record record);
    
    
    
    
    
    /**
     * defines a new name 
     * 
     * @param name        the name 
     * @param type        the value type
     * @param          the name type
     * @return a new instance
     */
    public static  ColumnName define(String name, Class type) {
        return new SkalarName<>(name, type);
    }

    
    /**
     * defines a new list name 
     * 
     * @param name          the name 
     * @param elementType   the list member value type
     * @param            the name type
     * @return a new instance
     */
    public static  ColumnName> defineList(String name, Class elementType) {
        return new ListName<>(name, elementType);
    }

    

    /**
     * defines a new set name 
     * 
     * @param name          the name 
     * @param elementType   the set member value type
     * @param            the name type
     * @return a new instance
     */
    public static  ColumnName> defineSet(String name, Class elementType) {
        return new SetName<>(name, elementType);
    }
        
    
    /**
     * defines a new set name 
     * 
     * @param name      the name 
     * @param keyType   the set member key type
     * @param valueType the set member value type
     * @param        the member key name type
     * @param        the member value type
     * @return a new instance
     */
    public static  ColumnName> defineMap(String name, Class keyType,  Class valueType) {
        return new MapName<>(name, keyType, valueType);
    }

 
    /**
     * defines a new name with Long-typed value
     * 
     * @param name        the name 
     * @return a new instance
     */
    public static ColumnName defineLong(String name) {
        return define(name, Long.class);
    }

    /**
     * defines a new name with String-typed value
     * 
     * @param name        the name 
     * @return a new instance
     */
    public static ColumnName defineString(String name) {
        return define(name, String.class);
    }
    
    /**
     * defines a new name with Boolean-typed value
     * 
     * @param name        the name 
     * @return a new instance
     */
    public static ColumnName defineBool(String name) {
        return define(name, Boolean.class);
    }
    
    /**
     * defines a new name with ByteBuffer-typed value
     * 
     * @param name        the name 
     * @return a new instance
     */
    public static ColumnName defineByteBuffer(String name) {
        return define(name, ByteBuffer.class);
    }
    
    /**
     * defines a new name with Float-typed value
     * 
     * @param name        the name 
     * @return a new instance
     */
    public static ColumnName defineFloat(String name) {
        return define(name, Float.class);
    }
   
    /**
     * defines a new name with Date-typed value
     * 
     * @param name        the name 
     * @return a new instance
     */
    public static ColumnName defineDate(String name) {
        return define(name, Date.class);
    }
    
    /**
     * defines a new name with Decimal-typed value
     * 
     * @param name   the name 
     * @return a new instance
     */
    public static ColumnName defineDecimal(String name) {
        return define(name, BigDecimal.class);
    }
    
    /**
     * defines a new name with Integer-typed value
     * 
     * @param name        the name 
     * @return a new instance
     */
    public static ColumnName defineInt(String name) {
        return define(name, Integer.class);
    }
    
    /**
     * defines a new name with InetAddress-typed value
     * 
     * @param name        the name 
     * @return a new instance
     */
    public static ColumnName defineInet(String name) {
        return define(name, InetAddress.class);
    }
    
    /**
     * defines a new name with Varint-typed value
     * 
     * @param name        the name 
     * @return a new instance
     */
    public static ColumnName defineVarint(String name) {
        return define(name, BigInteger.class);
    }
    
    /**
     * defines a new name with TupleValue-typed value
     * 
     * @param name        the name 
     * @return a new instance
     */
    public static ColumnName defineTupleValue(String name) {
        return define(name, TupleValue.class);
    }
    
    /**
     * defines a new name with UUID-typed value
     * 
     * @param name        the name 
     * @return a new instance
     */
    public static ColumnName defineUUID(String name) {
        return define(name, UUID.class);
    }
    

    /**
     * defines a new name with ByteBuffer-typed value
     * 
     * @param name        the name 
     * @return a new instance
     */
    public static ColumnName defineBytes(String name) {
        return define(name, byte[].class);
    }
    
    
    private static class SkalarName extends ColumnName {
        private final Class type;
        
        
        /**
         * @param name      the name
         * @param type      the value type
         */
        SkalarName(String name, Class type) {
            super(name);
            this.type = type;
        }
        
        @SuppressWarnings("unchecked")
        @Override
        T read(Record record) {
            return (T) record.getValue(getName(), (Class) type);
        }
    }
    

    
    /**
     * A list name which defines the class type of the associated list member value type 
     *
     * @param  the list member value type
     */
    private static class ListName extends ColumnName> {
        private final Class elementType;
        
        /**
         * @param name          the name
         * @param elementType   the list member value type
         */
        private ListName(String name, Class elementType) {
            super(name);
            this.elementType = elementType;
        }
        
        @Override
        ImmutableList read(Record record) {
            return record.getList(getName(), (Class) elementType);
        }
    }
    
    

     
    /**
     * A set name which defines the class type of the associated set member value type 
     *
     * @param  the set member value type
     */
    private static class SetName extends ColumnName> {
        private final Class elementType;

        /**
         * @param name          the name
         * @param elementType   the set member value type
         */
        private SetName(String name, Class elementType) {
            super(name);
            this.elementType = elementType;
        }
        
        @Override
        Set read(Record record) {
            return record.getSet(getName(), (Class) elementType);
        }
    } 
    
    
    /**
     * A set name which defines the class type of the associated set member value type 
     *
     * @param  the set member key type
     * @param  the set member value type
     */
    private static class MapName extends ColumnName> {
        
        private final Class keyType;
        private final Class valueType;
        
        
        /**
         * @param name      the name
         * @param keyType   the set member value type
         * @param valueType the set member value type 
         */
        private MapName(String name, Class keyType, Class valueType) {
            super(name);
            this.keyType = keyType;
            this.valueType = valueType;
        }
        
        @Override
        Map read(Record record) {
            return record.getMap(getName(), (Class) keyType, (Class) valueType);
        }
    } 
}