equal
deleted
inserted
replaced
1 /* |
1 /* |
2 * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
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 |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. Oracle designates this |
7 * published by the Free Software Foundation. Oracle designates this |
64 public class Track { |
64 public class Track { |
65 |
65 |
66 // TODO: use arrays for faster access |
66 // TODO: use arrays for faster access |
67 |
67 |
68 // the list containing the events |
68 // the list containing the events |
69 private ArrayList eventsList = new ArrayList(); |
69 private ArrayList<MidiEvent> eventsList = new ArrayList<>(); |
70 |
70 |
71 // use a hashset to detect duplicate events in add(MidiEvent) |
71 // use a hashset to detect duplicate events in add(MidiEvent) |
72 private HashSet set = new HashSet(); |
72 private HashSet<MidiEvent> set = new HashSet<>(); |
73 |
73 |
74 private MidiEvent eotEvent; |
74 private MidiEvent eotEvent; |
75 |
75 |
76 |
76 |
77 /** |
77 /** |
106 int eventsCount = eventsList.size(); |
106 int eventsCount = eventsList.size(); |
107 |
107 |
108 // get the last event |
108 // get the last event |
109 MidiEvent lastEvent = null; |
109 MidiEvent lastEvent = null; |
110 if (eventsCount > 0) { |
110 if (eventsCount > 0) { |
111 lastEvent = (MidiEvent) eventsList.get(eventsCount - 1); |
111 lastEvent = eventsList.get(eventsCount - 1); |
112 } |
112 } |
113 // sanity check that we have a correct end-of-track |
113 // sanity check that we have a correct end-of-track |
114 if (lastEvent != eotEvent) { |
114 if (lastEvent != eotEvent) { |
115 // if there is no eot event, add our immutable instance again |
115 // if there is no eot event, add our immutable instance again |
116 if (lastEvent != null) { |
116 if (lastEvent != null) { |
146 |
146 |
147 // insert event such that events is sorted in increasing |
147 // insert event such that events is sorted in increasing |
148 // tick order |
148 // tick order |
149 int i = eventsCount; |
149 int i = eventsCount; |
150 for ( ; i > 0; i--) { |
150 for ( ; i > 0; i--) { |
151 if (event.getTick() >= ((MidiEvent)eventsList.get(i-1)).getTick()) { |
151 if (event.getTick() >= (eventsList.get(i-1)).getTick()) { |
152 break; |
152 break; |
153 } |
153 } |
154 } |
154 } |
155 if (i == eventsCount) { |
155 if (i == eventsCount) { |
156 // we're adding an event after the |
156 // we're adding an event after the |
218 * @return the event at the specified index |
218 * @return the event at the specified index |
219 */ |
219 */ |
220 public MidiEvent get(int index) throws ArrayIndexOutOfBoundsException { |
220 public MidiEvent get(int index) throws ArrayIndexOutOfBoundsException { |
221 try { |
221 try { |
222 synchronized(eventsList) { |
222 synchronized(eventsList) { |
223 return (MidiEvent)eventsList.get(index); |
223 return eventsList.get(index); |
224 } |
224 } |
225 } catch (IndexOutOfBoundsException ioobe) { |
225 } catch (IndexOutOfBoundsException ioobe) { |
226 throw new ArrayIndexOutOfBoundsException(ioobe.getMessage()); |
226 throw new ArrayIndexOutOfBoundsException(ioobe.getMessage()); |
227 } |
227 } |
228 } |
228 } |
251 */ |
251 */ |
252 public long ticks() { |
252 public long ticks() { |
253 long ret = 0; |
253 long ret = 0; |
254 synchronized (eventsList) { |
254 synchronized (eventsList) { |
255 if (eventsList.size() > 0) { |
255 if (eventsList.size() > 0) { |
256 ret = ((MidiEvent)eventsList.get(eventsList.size() - 1)).getTick(); |
256 ret = (eventsList.get(eventsList.size() - 1)).getTick(); |
257 } |
257 } |
258 } |
258 } |
259 return ret; |
259 return ret; |
260 } |
260 } |
261 |
261 |