001 package com.croftsoft.apps.ajgp.data; 002 003 import java.beans.XMLDecoder; 004 import java.beans.XMLEncoder; 005 import java.io.ByteArrayInputStream; 006 import java.io.ByteArrayOutputStream; 007 import java.io.Serializable; 008 009 import com.croftsoft.core.lang.Testable; 010 011 /********************************************************************* 012 * Example data object for Data Persistence chapter. 013 * 014 * @version 015 * 2003-03-13 016 * @since 017 * 2003-03-12 018 * @author 019 * <a href="https://www.croftsoft.com/">David Wallace Croft</a> 020 *********************************************************************/ 021 022 public final class SerializableGameData 023 implements Serializable, GameData, Testable 024 ////////////////////////////////////////////////////////////////////// 025 ////////////////////////////////////////////////////////////////////// 026 { 027 028 private static final long serialVersionUID = 1L; 029 030 // 031 032 private int health; 033 034 private int wealth; 035 036 private int wisdom; 037 038 ////////////////////////////////////////////////////////////////////// 039 // static methods 040 ////////////////////////////////////////////////////////////////////// 041 042 public static void main ( String [ ] args ) 043 ////////////////////////////////////////////////////////////////////// 044 { 045 System.out.println ( test ( args ) ); 046 } 047 048 public static boolean test ( String [ ] args ) 049 ////////////////////////////////////////////////////////////////////// 050 { 051 try 052 { 053 final int TEST_HEALTH = -1; 054 055 GameData gameData1 = new SerializableGameData ( ); 056 057 System.out.println ( "health = " + gameData1.getHealth ( ) ); 058 059 System.out.println ( "wealth = " + gameData1.getWealth ( ) ); 060 061 System.out.println ( "wisdom = " + gameData1.getWisdom ( ) ); 062 063 gameData1.setHealth ( TEST_HEALTH ); 064 065 ByteArrayOutputStream byteArrayOutputStream 066 = new ByteArrayOutputStream ( ); 067 068 XMLEncoder xmlEncoder = new XMLEncoder ( byteArrayOutputStream ); 069 070 xmlEncoder.writeObject ( gameData1 ); 071 072 xmlEncoder.close ( ); 073 074 byte [ ] xmlBytes = byteArrayOutputStream.toByteArray ( ); 075 076 System.out.println ( new String ( xmlBytes ) ); 077 078 XMLDecoder xmlDecoder 079 = new XMLDecoder ( new ByteArrayInputStream ( xmlBytes ) ); 080 081 GameData gameData2 = ( GameData ) xmlDecoder.readObject ( ); 082 083 System.out.println ( "health = " + gameData2.getHealth ( ) ); 084 085 System.out.println ( "wealth = " + gameData2.getWealth ( ) ); 086 087 System.out.println ( "wisdom = " + gameData2.getWisdom ( ) ); 088 089 return gameData2.getHealth ( ) == TEST_HEALTH 090 && gameData2.getWealth ( ) == DEFAULT_WEALTH 091 && gameData2.getWisdom ( ) == DEFAULT_WISDOM; 092 } 093 catch ( Exception ex ) 094 { 095 ex.printStackTrace ( ); 096 097 return false; 098 } 099 } 100 101 ////////////////////////////////////////////////////////////////////// 102 // no-argument constructor method 103 ////////////////////////////////////////////////////////////////////// 104 105 public SerializableGameData ( ) 106 ////////////////////////////////////////////////////////////////////// 107 { 108 setHealth ( DEFAULT_HEALTH ); 109 110 setWealth ( DEFAULT_WEALTH ); 111 112 setWisdom ( DEFAULT_WISDOM ); 113 } 114 115 ////////////////////////////////////////////////////////////////////// 116 // accessor methods 117 ////////////////////////////////////////////////////////////////////// 118 119 public int getHealth ( ) { return health; } 120 121 public int getWealth ( ) { return wealth; } 122 123 public int getWisdom ( ) { return wisdom; } 124 125 ////////////////////////////////////////////////////////////////////// 126 // mutator methods 127 ////////////////////////////////////////////////////////////////////// 128 129 public void setHealth ( int health ) 130 ////////////////////////////////////////////////////////////////////// 131 { 132 if ( health < MINIMUM_HEALTH ) 133 { 134 throw new IllegalArgumentException ( "health < minimum" ); 135 } 136 137 this.health = health; 138 } 139 140 public void setWealth ( int wealth ) 141 ////////////////////////////////////////////////////////////////////// 142 { 143 if ( wealth < MINIMUM_WEALTH ) 144 { 145 throw new IllegalArgumentException ( "wealth < minimum" ); 146 } 147 148 this.wealth = wealth; 149 } 150 151 public void setWisdom ( int wisdom ) 152 ////////////////////////////////////////////////////////////////////// 153 { 154 if ( wisdom < MINIMUM_WISDOM ) 155 { 156 throw new IllegalArgumentException ( "wisdom < minimum" ); 157 } 158 159 this.wisdom = wisdom; 160 } 161 162 ////////////////////////////////////////////////////////////////////// 163 ////////////////////////////////////////////////////////////////////// 164 }