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

it.tidalwave.netbeans.persistence.maintenance.impl.EntityAndTablePair Maven / Gradle / Ivy

The newest version!
/***********************************************************************************************************************
 *
 * OpenBlueSky - NetBeans Platform Enhancements
 * Copyright (C) 2006-2012 by Tidalwave s.a.s. (http://www.tidalwave.it)
 *
 ***********************************************************************************************************************
 *
 * 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.
 *
 ***********************************************************************************************************************
 *
 * WWW: http://openbluesky.java.net
 * SCM: https://bitbucket.org/tidalwave/openbluesky-src
 *
 **********************************************************************************************************************/
package it.tidalwave.netbeans.persistence.maintenance.impl;

import javax.persistence.Entity;
import javax.persistence.Table;

/***********************************************************************************************************************
 *
 * An aggregate of a entity class and a table name.
 *
 * @author  Fabrizio Giudici
 * @version $Id$
 *
 **********************************************************************************************************************/
public class EntityAndTablePair implements Comparable
  {
    private final Class entityClass;
    private final String tableName;

    /***************************************************************************
     *
     *
     **************************************************************************/
    public EntityAndTablePair (final Class entityClass, final String tableName) 
      {
        if (entityClass == null) 
          {
            throw new IllegalArgumentException("entityClass is mandatory");
          }
        
        if (tableName == null) 
          {
            throw new IllegalArgumentException("tableName is mandatory");
          }

        final Entity entity = entityClass.getAnnotation(Entity.class);
        
        if (entity == null)
          {
            throw new IllegalArgumentException("entityClass must be annotated with @Entity");
          }
            
        final Table table = entityClass.getAnnotation(Table.class);
        
        if (table == null)
          {
            throw new IllegalArgumentException("entityClass must be annotated with @Table");
          }
            
        this.entityClass = entityClass;
        this.tableName = tableName;
      }

    /***************************************************************************
     *
     * Returns the entity class.
     * 
     * @return  the entity class
     *
     **************************************************************************/
    public Class getEntityClass() 
      {
        return entityClass;
      }

    /***************************************************************************
     *
     * Returns the table name.
     * 
     * @return  the table name
     *
     **************************************************************************/
    public String getTableName() 
      {
        return tableName;
      }

    /***************************************************************************
     *
     * {@inheritDoc}
     *
     **************************************************************************/
    public int compareTo (final EntityAndTablePair other) 
      {
        final int d1 = entityClass.getName().compareTo(other.entityClass.getName());

        if (d1 != 0) 
          {
            return d1;
          }

        return tableName.compareTo(other.tableName);
      }

    /***************************************************************************
     *
     * {@inheritDoc}
     *
     **************************************************************************/
    @Override
    public String toString() 
      {
        return String.format("[%s,%s]", entityClass.getName(), tableName);
      }

    /***************************************************************************
     *
     * {@inheritDoc}
     *
     **************************************************************************/
    @Override
    public boolean equals (final Object object) 
      {
        if (object == null) 
          {
            return false;
          }

        if (getClass() != object.getClass()) 
          {
            return false;
          }
        
        final EntityAndTablePair other = (EntityAndTablePair) object;

        if (!this.entityClass.equals(other.entityClass)) 
          {
            return false;
          }

        if (!this.tableName.equals(other.tableName)) 
          {
            return false;
          }

        return true;
      }

    /***************************************************************************
     *
     * {@inheritDoc}
     *
     **************************************************************************/
    @Override
    public int hashCode() 
      {
        int hash = 7;
        hash = 53 * hash + this.entityClass.hashCode();
        hash = 53 * hash + this.tableName.hashCode();
        return hash;
      }
  }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy