john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

Report CloudStorageGateway

// 2012-10-13 johnpfeiffer storagegatewaymanager.PstCloudGateway
// TODO: unit tests

package com.oxygencloud.vm;


import java.sql.Timestamp;
import java.util.Comparator;
import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.constraints.NotNull;

public class CloudStorageGateway
{
    public static final String CLASSVERSION = "0.3";
    protected static final String OIDNULLERROR = "ERROR: Oid cannot be null";
    protected static final String OIDEMPTYERROR = "ERROR: Oid cannot be empty";
    protected static final String HASHNULLERROR = "ERROR: Hash cannot be null";
    protected static final String HASHEMPTYERROR = "ERROR: Hash cannot be empty";
    protected static final String ADDRESSNULLERROR = "ERROR: Address cannot be null";
    protected static final String ADDRESSEMPTYERROR = "ERROR: Address cannot be empty";
    protected static final String DEVICESERIALNULLERROR = "ERROR: Device Serial Number cannot be null";
    protected static final String DEVICESERIALEMPTYERROR= "ERROR: Device Serial Number cannot be empty";


    @NotNull( message = OIDNULLERROR )
    private String oid;
    @NotNull( message = HASHNULLERROR )
    private String hash;
    @NotNull( message = ADDRESSNULLERROR )
    private String address;
    @NotNull( message = DEVICESERIALNULLERROR )
    private String deviceSerialNumber;

    private boolean connected;
    private Timestamp lastModified;
    private boolean deleted;

    public static class Builder
    {
        private String oid;
        private String hash;
        private String address;
        private String deviceSerialNumber;
        private boolean connected;
        private Timestamp lastModified;
        private boolean deleted;

        public Builder()
        {
        }

        public Builder oid( String oid )
        {
            this.oid = oid;
            return this;
        }

        public Builder hash( String hash )
        {
            this.hash = hash;
            return this;
        }

        public Builder address( String address )
        {
            this.address = address;
            return this;
        }

        public Builder deviceSerialNumber( String deviceSerialNumber )
        {
            this.deviceSerialNumber = deviceSerialNumber;
            return this;
        }

        public Builder connected( boolean connected )
        {
            this.connected = connected;
            return this;
        }

        public Builder lastModified( Timestamp lastModified )
        {
            this.lastModified = lastModified;
            return this;
        }

        public Builder deleted( boolean deleted )
        {
            this.deleted = deleted;
            return this;
        }

        public CloudStorageGateway build() throws IllegalArgumentException
        {
            CloudStorageGateway cloudSGW = new CloudStorageGateway( this ); // object may exist in an incomplete or illegal state
            CloudStorageGatewayValidator( cloudSGW );
            return cloudSGW;
        }

        private static Validator validator = Validation.buildDefaultValidatorFactory().getValidator();

        private void CloudStorageGatewayValidator( CloudStorageGateway cloudSGW ) throws IllegalArgumentException
        {
            Set <ConstraintViolation <CloudStorageGateway>> violations = validator.validate( cloudSGW );
            if( !violations.isEmpty() )
            {
                for( ConstraintViolation <?> v : violations )
                {
                    throw new IllegalArgumentException( v.getMessage() );
                }
            }

            if( this.oid.isEmpty() )
            {
                throw new IllegalArgumentException( OIDEMPTYERROR );
            }
            if( this.hash.isEmpty() )
            {
                throw new IllegalArgumentException( HASHEMPTYERROR );
            }
            if( this.address.isEmpty() )
            {
                throw new IllegalArgumentException( ADDRESSEMPTYERROR );
            }
            if( this.deviceSerialNumber.isEmpty() )
            {
                throw new IllegalArgumentException( DEVICESERIALEMPTYERROR );
            }

        }
    } // end inner class Builder

    private CloudStorageGateway( Builder builder ) throws IllegalArgumentException
    {
        this.oid = builder.oid;
        this.hash = builder.hash;
        this.address = builder.address;
        this.deviceSerialNumber = builder.deviceSerialNumber;
        this.connected = builder.connected;
        this.lastModified = builder.lastModified;
        this.deleted = builder.deleted;
    }

    String getOid()
    {
        return oid;
    }

    String getHash()
    {
        return hash;
    }

    String getAddress()
    {
        return address;
    }

    String getDeviceSerialNumber()
    {
        return deviceSerialNumber;
    }

    boolean isConnected()
    {
        return connected;
    }

    Timestamp getLastModified()
    {
        return lastModified;
    }

    boolean isDeleted()
    {
        return deleted;
    }

    static Comparator <CloudStorageGateway> COMPARE_BY_ADDRESS = new Comparator <CloudStorageGateway>()
    {
        public int compare( CloudStorageGateway first , CloudStorageGateway other )
        {
            return first.address.compareTo( other.address );
        }
    };
    static Comparator <CloudStorageGateway> COMPARE_BY_LASTMODIFIED = new Comparator <CloudStorageGateway>()
    {
        public int compare( CloudStorageGateway first , CloudStorageGateway other )
        {
            return first.lastModified.compareTo( other.lastModified );
        }
    };

    // further consideration Collections.sort(list, String.CASE_INSENSITIVE_ORDER); , Collections.sort(list, Collections.reverseOrder());

} // end class

  • « junit spring dependency injection list
  • Report CloudStorageGatewayTest »

Published

Oct 14, 2012

Category

java-servlet

~408 words

Tags

  • cloudstoragegateway 1
  • java-servlet 61
  • report 14