[152] | 1 | package solarsim.common; |
---|
| 2 | |
---|
| 3 | import java.io.BufferedReader; |
---|
| 4 | import solarsim.io.MyInputStreamReader; |
---|
| 5 | import solarsim.*; |
---|
| 6 | import java.io.ByteArrayInputStream; |
---|
| 7 | import java.io.IOException; |
---|
| 8 | import java.io.InputStream; |
---|
| 9 | import java.io.InputStreamReader; |
---|
| 10 | import java.util.*; |
---|
| 11 | |
---|
| 12 | /** |
---|
| 13 | * Clasa care descrie starea sistemului la un moment dat. Contine o lista de obiecte {@link Particle} |
---|
| 14 | * care specifica masele, pozitiile si vitezele elementelor sistemului. |
---|
| 15 | * @author Marius Ion |
---|
| 16 | */ |
---|
| 17 | public class State { |
---|
| 18 | |
---|
| 19 | private enum FP_Precision { |
---|
| 20 | FLOAT, DOUBLE; |
---|
| 21 | }; |
---|
| 22 | |
---|
| 23 | private List<Particle> particles; |
---|
| 24 | private static final FP_Precision precision = FP_Precision.FLOAT; |
---|
| 25 | |
---|
| 26 | private State(){} |
---|
| 27 | |
---|
| 28 | public State(List<Particle> particles) { |
---|
| 29 | this.particles = particles; |
---|
| 30 | } |
---|
| 31 | |
---|
| 32 | /** |
---|
| 33 | * Metoda statica ce permite citirea starii dintr-un buffer. Starea a fost |
---|
| 34 | * scrisa in buffer de catre programul C. |
---|
| 35 | * @param inputStream streamul din care se citeste |
---|
| 36 | * @return starea sistemului |
---|
| 37 | * @throws IOException cand s-a produs o eroare la citirea din InputStream |
---|
| 38 | */ |
---|
| 39 | public static State readStateFromBuffer(byte[] data, int offset, int length) |
---|
| 40 | throws IOException { |
---|
| 41 | ByteArrayInputStream ins = new ByteArrayInputStream(data, offset, length); |
---|
| 42 | return readStateFromInputStream(ins); |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | /** |
---|
| 46 | * Metoda statica ce permite citirea starii dintr-un InputStream. Starea a fost |
---|
| 47 | * scrisa in stream de catre programul C. |
---|
| 48 | * @param inputStream streamul din care se citeste |
---|
| 49 | * @return starea sistemului |
---|
| 50 | * @throws IOException cand s-a produs o eroare la citirea din InputStream |
---|
| 51 | */ |
---|
| 52 | public static State readStateFromInputStream(InputStream inputStream) |
---|
| 53 | throws IOException { |
---|
| 54 | State state = new State(); |
---|
| 55 | MyInputStreamReader in = new MyInputStreamReader(inputStream); |
---|
| 56 | |
---|
| 57 | int n = in.readInt(); |
---|
| 58 | if( n == 0 ) |
---|
| 59 | return null; |
---|
| 60 | state.particles = new ArrayList<Particle>(n); |
---|
| 61 | for( int i = 0; i < n; i++ ) { |
---|
| 62 | state.particles.add(new Particle()); |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | for( int i = 0; i < n; i++ ) { |
---|
| 66 | double mass; |
---|
| 67 | if( precision == FP_Precision.DOUBLE ) |
---|
| 68 | mass = in.readDouble(); |
---|
| 69 | else |
---|
| 70 | mass = (double)in.readFloat(); |
---|
| 71 | state.particles.get(i).mass = mass; |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | for( int i = 0; i < n; i++ ) { |
---|
| 75 | Vector position; |
---|
| 76 | if( precision == FP_Precision.DOUBLE ) |
---|
| 77 | position = in.readVector(); |
---|
| 78 | else |
---|
| 79 | position = in.readFloatVector(); |
---|
| 80 | state.particles.get(i).position = position; |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | for( int i = 0; i < n; i++ ) { |
---|
| 84 | Vector velocity; |
---|
| 85 | if( precision == FP_Precision.DOUBLE ) |
---|
| 86 | velocity = in.readVector(); |
---|
| 87 | else |
---|
| 88 | velocity = in.readFloatVector(); |
---|
| 89 | state.particles.get(i).velocity = velocity; |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | return state; |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | /** |
---|
| 96 | * Getter pentru particule. |
---|
| 97 | * @return lista particulelor din sistem. |
---|
| 98 | */ |
---|
| 99 | public List<Particle> getParticles() { |
---|
| 100 | return particles; |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | @Override |
---|
| 104 | public String toString() { |
---|
| 105 | StringBuffer buf = new StringBuffer(); |
---|
| 106 | |
---|
| 107 | for( int i = 0; i < particles.size(); i++ ) { |
---|
| 108 | buf.append("Particle " + i + ":\n"); |
---|
| 109 | buf.append("\tmass:" + particles.get(i).mass + "\n"); |
---|
| 110 | buf.append("\tposition:" + particles.get(i).position + "\n"); |
---|
| 111 | buf.append("\tvelocity:" + particles.get(i).velocity + "\n"); |
---|
| 112 | buf.append("\n"); |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | return buf.toString(); |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | } |
---|