1 /*
   2  * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package util;
  24 
  25 import java.io.Serializable;
  26 import java.sql.SQLData;
  27 import java.sql.SQLException;
  28 import java.sql.SQLInput;
  29 import java.sql.SQLOutput;
  30 
  31 public class SuperHero implements SQLData, Serializable {
  32 
  33     private String first;
  34     private String last;
  35     private final String type;
  36     private Integer firstYear;
  37     private String secretIdentity;
  38 
  39     public SuperHero(String sqlType, String fname, String lname, Integer year,
  40             String identity) {
  41         first = fname;
  42         last = lname;
  43         type = sqlType;
  44         firstYear = year;
  45         secretIdentity = identity;
  46     }
  47 
  48     @Override
  49     public String getSQLTypeName() throws SQLException {
  50         return type;
  51     }
  52 
  53     @Override
  54     public void readSQL(SQLInput stream, String typeName) throws SQLException {
  55         first = stream.readString();
  56         last = stream.readString();
  57         firstYear = stream.readInt();
  58         secretIdentity = stream.readString();
  59     }
  60 
  61     @Override
  62     public void writeSQL(SQLOutput stream) throws SQLException {
  63         stream.writeString(first);
  64         stream.writeString(last);
  65         stream.writeInt(firstYear);
  66         stream.writeString(secretIdentity);
  67     }
  68 
  69     @Override
  70     public String toString() {
  71         return "[ name =" + first + " " + last + " "
  72                 + firstYear + " " + secretIdentity + " ]";
  73     }
  74 
  75     public void setIdentity(String identity) {
  76         secretIdentity = identity;
  77     }
  78 
  79     public String getIdentity() {
  80         return secretIdentity;
  81     }
  82 
  83     @Override
  84     public boolean equals(Object obj) {
  85         if (this == obj) {
  86             return true;
  87         }
  88         if (obj instanceof SuperHero) {
  89             SuperHero ss = (SuperHero) obj;
  90             return first.equals(ss.first) && last.equals(ss.last)
  91                     && firstYear.equals(ss.firstYear)
  92                     && type.equals(ss.type)
  93                     && secretIdentity.equals(ss.secretIdentity);
  94         }
  95         return false;
  96     }
  97 
  98     @Override
  99     public int hashCode() {
 100         return ((31 + first.hashCode()) * 31) * 31
 101                 + ((31 + last.hashCode()) * 31) * 31
 102                 + ((31 + firstYear.hashCode()) * 31) * 31
 103                 + ((31 + type.hashCode()) * 31) * 31
 104                 + secretIdentity.hashCode();
 105     }
 106 }