public boolean equals(Clock another_clock) { // Check if 'this' is equal to 'another_clock' // 1. If you're checking if the pointer is the same return another_clock.equals(this); // 2. If you're checking if time is the same (You probably need to create getter/setter methods or change privacy for these fields) return another_clock.hours == this.hours && another_clock.minutes == this.minutes && another_clock.seconds == this.seconds; }
@Override public Clock deserialize(byte[] bytes) throws SerializationException { return Clock.parseClock(bytes); }
@Override public byte[] serialize(Clock clock) throws SerializationException { return clock == null ? null : clock.toByteArray(); } }
@Override public long getOffset(Clock sinceClock) { if(_minClock.before(sinceClock) && !sinceClock.after(_maxClock)) { int i = 0; for(; i < _events.size(); i++) { Event<T> e = _events.get(i); Occurred occ = sinceClock.compareTo(e.getClock()); if(occ == Occurred.EQUICONCURRENTLY) { break; } else if(occ == Occurred.BEFORE || occ == Occurred.CONCURRENTLY) { i--; break; } } return _origin + i; } return -1; }
/** * @return <code>true</code> if this Clock occurred after the specified Clock <code>c</code>. * Otherwise, <code>false</code>. */ public boolean after(Clock c) { return compareTo(c) == Occurred.AFTER; }
/** * Parses a Clock value from its raw bytes. * * @param raw - the raw bytes of Clock * @return a Clock object. * <code>Clock.ZERO</code> is returned upon <code>null</code> or a byte array with the length less than 8. */ public static Clock parseClock(byte[] raw) { if(raw == null || raw.length < 8) { return Clock.ZERO; } int cnt = raw.length >> 3; long[] values = new long[cnt]; ByteBuffer bb = ByteBuffer.wrap(raw); for(int i = 0; i < values.length; i++) { values[i] = bb.getLong(); } return new Clock(values); }
@Override public boolean put(Event<T> event) { Clock clock = event.getClock(); int size = _events.size(); if(size == 0 && _minClock.before(clock)) { _minClock = clock; _maxClock = clock; } if(size < _capacity && _maxClock.beforeEqual(clock)) { _events.add(event); _maxClock = clock; return true; } return false; }
@Override public int hashCode() { int hash = 5; hash = 97 * hash + _id; hash = 97 * hash + (int)_offset; hash = 97 * hash + _index; hash = 97 * hash + _clock.hashCode(); return hash; }
if(posClock.beforeEqual(evtClock)) { list.add(new SimpleEvent<T>(e.getKey(), evtClock)); cnt++; if(posClock.beforeEqual(evtClock)) { list.add(new SimpleEvent<T>(e.getKey(), evtClock)); cnt++;
@Override public Position getPosition(Clock clock) { if(clock == null) { throw new NullPointerException("clock"); } Schema schema = _protocol.getMessages().get(ProtocolConstants.MSG_META).getRequest(); GenericRecord req = new GenericData.Record(schema); req.put("src", _sourceUtf8); req.put("opt", new Utf8(StoreBusDirective.Position.toString())); req.put("key", new Utf8(clock.toString())); req.put("value", null); try { Utf8 res = (Utf8)send(ProtocolConstants.MSG_META, req); return res == null ? null : getPosition(res.toString()); } catch (IOException e) { throw new RuntimeException(e); } }
@Override public long getOffset(Clock sinceClock) { if(_minClock.before(sinceClock) && !sinceClock.after(_maxClock)) { int i = 0; for(; i < _events.size(); i++) { Event<T> e = _events.get(i); Occurred occ = sinceClock.compareTo(e.getClock()); if(occ == Occurred.EQUICONCURRENTLY) { break; } else if(occ == Occurred.BEFORE || occ == Occurred.CONCURRENTLY) { i--; break; } } return _origin + i; } return -1; }
/** * @return <code>true</code> if this Clock is equal to or occurred after the specified Clock <code>c</code>. * Otherwise, <code>false</code>. */ public boolean afterEqual(Clock c) { Occurred o = compareTo(c); return o == Occurred.AFTER || o == Occurred.EQUICONCURRENTLY; }
/** * Parses a Clock value from its raw bytes. * * @param raw - the raw bytes of Clock * @return a Clock object. * <code>Clock.ZERO</code> is returned upon <code>null</code> or a byte array with the length less than 8. */ public static Clock parseClock(byte[] raw) { if(raw == null || raw.length < 8) { return Clock.ZERO; } int cnt = raw.length >> 3; long[] values = new long[cnt]; ByteBuffer bb = ByteBuffer.wrap(raw); for(int i = 0; i < values.length; i++) { values[i] = bb.getLong(); } return new Clock(values); }
@Override public boolean put(Event<T> event) { Clock clock = event.getClock(); int size = _events.size(); if(size == 0 && _minClock.before(clock)) { _minClock = clock; _maxClock = clock; } if(size < _capacity && _maxClock.beforeEqual(clock)) { _events.add(event); _maxClock = clock; return true; } return false; }
@Override public int hashCode() { int hash = 5; hash = 97 * hash + _id; hash = 97 * hash + (int)_offset; hash = 97 * hash + _index; hash = 97 * hash + _clock.hashCode(); return hash; }
if(posClock.beforeEqual(evtClock)) { list.add(new SimpleEvent<T>(e.getKey(), evtClock)); cnt++; if(posClock.beforeEqual(evtClock)) { list.add(new SimpleEvent<T>(e.getKey(), evtClock)); cnt++;
long sinceOffset; Occurred occ = sinceClock.compareTo(getMinClock()); if(occ == Occurred.EQUICONCURRENTLY) { return new SimplePosition(getId(), getOrigin(), getMinClock()); if(sinceClock.after(getMaxClock())) { return getPosition(); if(b2.getMaxClock().before(sinceClock)) { if(b1.getMinClock().compareTo(sinceClock) == Occurred.EQUICONCURRENTLY) { return new SimplePosition(getId(), b1.getOrigin(), b1.getMinClock()); } else { EventBatchHeader h = c.getHeader(); occ = h.getMinClock().compareTo(sinceClock); if(occ == Occurred.EQUICONCURRENTLY) { if(cnt == 0) { if(!sinceClock.after(h.getMaxClock())) { byte[] dat = _store.get(c.getLookup()); try {
@Override public Clock deserialize(byte[] bytes) throws SerializationException { return Clock.parseClock(bytes); }
public boolean equals (Clock c) { boolean equiv = c.equals(c); // this keeps calling itself return equiv; }
@Override public byte[] serialize(Clock clock) throws SerializationException { return clock == null ? null : clock.toByteArray(); } }