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 String type = "SUPERHERO"; 36 private Integer firstYear; 37 private String secretIdentity; 38 SuperHero()39 public SuperHero() { 40 41 } 42 SuperHero(String sqlType, String fname, String lname, Integer year, String identity)43 public SuperHero(String sqlType, String fname, String lname, Integer year, 44 String identity) { 45 first = fname; 46 last = lname; 47 type = sqlType; 48 firstYear = year; 49 secretIdentity = identity; 50 } 51 52 @Override getSQLTypeName()53 public String getSQLTypeName() throws SQLException { 54 return type; 55 } 56 57 @Override readSQL(SQLInput stream, String typeName)58 public void readSQL(SQLInput stream, String typeName) throws SQLException { 59 first = stream.readString(); 60 last = stream.readString(); 61 firstYear = stream.readInt(); 62 secretIdentity = stream.readString(); 63 } 64 65 @Override writeSQL(SQLOutput stream)66 public void writeSQL(SQLOutput stream) throws SQLException { 67 stream.writeString(first); 68 stream.writeString(last); 69 stream.writeInt(firstYear); 70 stream.writeString(secretIdentity); 71 } 72 73 @Override toString()74 public String toString() { 75 return "[ name =" + first + " " + last + " " 76 + firstYear + " " + secretIdentity + " ]"; 77 } 78 setIdentity(String identity)79 public void setIdentity(String identity) { 80 secretIdentity = identity; 81 } 82 getIdentity()83 public String getIdentity() { 84 return secretIdentity; 85 } 86 87 @Override equals(Object obj)88 public boolean equals(Object obj) { 89 if (this == obj) { 90 return true; 91 } 92 if (obj instanceof SuperHero) { 93 SuperHero ss = (SuperHero) obj; 94 return first.equals(ss.first) && last.equals(ss.last) 95 && firstYear.equals(ss.firstYear) 96 && type.equals(ss.type) 97 && secretIdentity.equals(ss.secretIdentity); 98 } 99 return false; 100 } 101 102 @Override hashCode()103 public int hashCode() { 104 return ((31 + first.hashCode()) * 31) * 31 105 + ((31 + last.hashCode()) * 31) * 31 106 + ((31 + firstYear.hashCode()) * 31) * 31 107 + ((31 + type.hashCode()) * 31) * 31 108 + secretIdentity.hashCode(); 109 } 110 } 111