source: trunk/src/org/apollo/meldex/StandardisedMelody.java

Last change on this file was 1102, checked in by davidb, 6 years ago

Reworking of the code-base to separate logic from graphics. This version of Expeditee now supports a JFX graphics as an alternative to SWING

File size: 2.5 KB
Line 
1package org.apollo.meldex;
2
3import java.io.FileInputStream;
4import java.io.FileNotFoundException;
5import java.io.FileOutputStream;
6import java.io.IOException;
7import java.io.ObjectInputStream;
8import java.io.ObjectOutputStream;
9import java.io.Serializable;
10
11
12/**
13 */
14public class StandardisedMelody implements Serializable
15{
16
17 private static final long serialVersionUID = -7246438481007658500L;
18
19 /** The array of MelodyEvents that form this melody. */
20 private MelodyEvent[] events;
21
22
23 /**
24 * Creates a new StandardisedMelody with the given melody events.
25 *
26 * @param events The melody events that form the melody.
27 */
28 public StandardisedMelody(MelodyEvent[] events)
29 {
30 this.events = events;
31 }
32
33
34 /**
35 * Returns the length (number of melody events) of this melody.
36 *
37 * @return the length (number of melody events) of this melody.
38 */
39 public int getLength()
40 {
41 return events.length;
42 }
43
44
45 /**
46 * Returns a specified melody event from the melody.
47 *
48 * @param index The index of the required melody event.
49 *
50 * @return The melody event at the specified index.
51 */
52 public MelodyEvent getEvent(int index)
53 {
54 return events[index];
55 }
56
57
58 /**
59 */
60 public void setEvent(int index, MelodyEvent event)
61 {
62 events[index] = event;
63 }
64
65
66 /**
67 */
68 public static void writeMelodyToFile(String melodyFileName, StandardisedMelody melody)
69 throws FileNotFoundException, IOException
70 {
71 // Open the melody file for writing serialized objects
72 FileOutputStream fos = new FileOutputStream(melodyFileName);
73 ObjectOutputStream melodyFileStream = new ObjectOutputStream(fos);
74
75 // Write the melody to the melody file
76 melodyFileStream.writeObject(melody);
77
78 // Flush and close the collection file
79 melodyFileStream.flush();
80 melodyFileStream.close();
81
82 }
83
84
85 /**
86 */
87 public static StandardisedMelody readMelodyFromFile(String melodyFileName)
88 throws FileNotFoundException, IOException, ClassNotFoundException
89 {
90 // Open the melody file for reading serialized objects
91 FileInputStream fis = new FileInputStream(melodyFileName);
92 ObjectInputStream melodyFileStream = new ObjectInputStream(fis);
93
94 // Read the melody from the melody file
95 StandardisedMelody standardisedMelody = (StandardisedMelody) melodyFileStream.readObject();
96
97 melodyFileStream.close();
98
99 return standardisedMelody;
100
101 }
102}
Note: See TracBrowser for help on using the repository browser.