Skip to content

Commit

Permalink
Use mini2Dx-lock-provider library. Update to Java 8
Browse files Browse the repository at this point in the history
  • Loading branch information
tomcashman committed Feb 11, 2021
1 parent 6398110 commit 8b671be
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 113 deletions.
4 changes: 4 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
[1.7.0]
- Changed to using mini2Dx-lock-provider library to support game consoles
- Source/target compatability now Java 8

[1.6.0]
- Add EntityMessageData for transportation of entities in an Entity-Component-System pattern
- Allow for cancellation of messages in the bus queue
Expand Down
6 changes: 4 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ repositories {
maven { url "https://mini2dx.org/maven/content/repositories/thirdparty" }
}

sourceCompatibility = 1.7
targetCompatibility = 1.7
sourceCompatibility = 1.8
targetCompatibility = 1.8

task javadocJar(type: Jar) {
classifier = 'javadoc'
Expand All @@ -54,6 +54,8 @@ artifacts {
}

dependencies {
compile "org.mini2Dx:mini2Dx-lock-provider:1.1.0"

testCompile 'junit:junit:4.11'
testCompile "org.jmock:jmock-junit4:2.5.1"
testCompile "org.jmock:jmock-legacy:2.5.1"
Expand Down
9 changes: 3 additions & 6 deletions src/main/java/org/mini2Dx/minibus/MessageBus.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@
*/
package org.mini2Dx.minibus;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import org.mini2Dx.lockprovider.Locks;
import org.mini2Dx.lockprovider.jvm.JvmLocks;
import org.mini2Dx.minibus.exchange.ConcurrentMessageExchange;
import org.mini2Dx.minibus.exchange.ImmediateMessageExchange;
import org.mini2Dx.minibus.exchange.IntervalMessageExchange;
Expand All @@ -36,15 +35,13 @@
import org.mini2Dx.minibus.exchange.query.QueryMessageExchangePool;
import org.mini2Dx.minibus.transmission.MessageTransmission;
import org.mini2Dx.minibus.transmission.MessageTransmissionPool;
import org.mini2Dx.minibus.util.JvmLockProvider;
import org.mini2Dx.minibus.util.LockProvider;
import org.mini2Dx.minibus.util.SnapshotArrayList;

/**
* A message bus to publishing {@link MessageData}s
*/
public class MessageBus {
public static LockProvider LOCK_PROVIDER = new JvmLockProvider();
public static Locks LOCK_PROVIDER = new JvmLocks();

final List<MessageExchange> exchangers = new SnapshotArrayList<MessageExchange>();
final List<CancelledMessageHandler> cancelledMessageHandlers = new SnapshotArrayList<CancelledMessageHandler>();
Expand Down
42 changes: 0 additions & 42 deletions src/main/java/org/mini2Dx/minibus/util/JvmLockProvider.java

This file was deleted.

34 changes: 0 additions & 34 deletions src/main/java/org/mini2Dx/minibus/util/LockProvider.java

This file was deleted.

58 changes: 29 additions & 29 deletions src/main/java/org/mini2Dx/minibus/util/SnapshotArrayList.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
*/
package org.mini2Dx.minibus.util;

import org.mini2Dx.lockprovider.ReadWriteLock;
import org.mini2Dx.minibus.MessageBus;

import java.util.*;
import java.util.concurrent.locks.ReadWriteLock;

public class SnapshotArrayList<T> implements List<T> {
private final Object iteratorLock = new Object();
Expand Down Expand Up @@ -59,9 +59,9 @@ private void ensureCapacity(int capacity) {
@Override
public int size() {
final int result;
lock.readLock().lock();
lock.lockRead();
result = size;
lock.readLock().unlock();
lock.unlockRead();
return result;
}

Expand All @@ -73,14 +73,14 @@ public boolean isEmpty() {
@Override
public boolean contains(Object o) {
boolean result = false;
lock.readLock().lock();
lock.lockRead();
for(int i = 0; i < size; i++) {
if(array[i].equals(o)) {
result = true;
break;
}
}
lock.readLock().unlock();
lock.unlockRead();
return result;
}

Expand All @@ -94,9 +94,9 @@ public Iterator<T> iterator() {
result = iteratorPool.poll();
}
}
lock.readLock().lock();
lock.lockRead();
result.init(array, size);
lock.readLock().unlock();
lock.unlockRead();
return result;
}

Expand All @@ -112,19 +112,19 @@ public <T1> T1[] toArray(T1[] a) {

@Override
public boolean add(T t) {
lock.writeLock().lock();
lock.lockWrite();
ensureCapacity(size + 1);
array[size] = t;
size++;
lock.writeLock().unlock();
lock.unlockWrite();
return true;
}

@Override
public boolean remove(Object o) {
boolean result = false;

lock.writeLock().lock();
lock.lockWrite();
for(int i = 0; i < size; i++) {
if(!array[i].equals(o)) {
continue;
Expand All @@ -141,7 +141,7 @@ public boolean remove(Object o) {
result = true;
break;
}
lock.writeLock().unlock();
lock.unlockWrite();

return result;
}
Expand All @@ -158,9 +158,9 @@ public boolean containsAll(Collection<?> c) {

@Override
public boolean addAll(Collection<? extends T> c) {
lock.writeLock().lock();
lock.lockWrite();
ensureCapacity(size + c.size());
lock.writeLock().unlock();
lock.unlockWrite();
for(T o : c) {
add(o);
}
Expand Down Expand Up @@ -188,28 +188,28 @@ public boolean retainAll(Collection<?> c) {

@Override
public void clear() {
lock.writeLock().lock();
lock.lockWrite();
for(int i = 0; i < array.length; i++) {
array[i] = null;
}
size = 0;
lock.writeLock().unlock();
lock.unlockWrite();
}

@Override
public T get(int index) {
T result = null;
lock.readLock().lock();
lock.lockRead();
if(index < 0) {
lock.readLock().unlock();
lock.unlockRead();
throw new IndexOutOfBoundsException();
}
if(index >= size) {
lock.readLock().unlock();
lock.unlockRead();
throw new IndexOutOfBoundsException();
}
result = (T) array[index];
lock.readLock().unlock();
lock.unlockRead();
return result;
}

Expand All @@ -225,16 +225,16 @@ public void add(int index, T element) {

private T remove(int index, boolean throwException) {
Object result = null;
lock.writeLock().lock();
lock.lockWrite();
if(index < 0) {
lock.writeLock().unlock();
lock.unlockWrite();
if(throwException) {
throw new IndexOutOfBoundsException();
}
return null;
}
if(index >= size) {
lock.writeLock().unlock();
lock.unlockWrite();
if(throwException) {
throw new IndexOutOfBoundsException();
}
Expand All @@ -250,7 +250,7 @@ private T remove(int index, boolean throwException) {
}

size--;
lock.writeLock().unlock();
lock.unlockWrite();
return (T) result;
}

Expand All @@ -265,27 +265,27 @@ public T remove(int index) {

@Override
public int indexOf(Object o) {
lock.readLock().lock();
lock.lockRead();
for(int i = 0; i < size; i++) {
if(array[i].equals(o)) {
lock.readLock().unlock();
lock.unlockRead();
return i;
}
}
lock.readLock().unlock();
lock.unlockRead();
return -1;
}

@Override
public int lastIndexOf(Object o) {
lock.readLock().lock();
lock.lockRead();
for(int i = size - 1; i >= 0; i--) {
if(array[i].equals(o)) {
lock.readLock().unlock();
lock.unlockRead();
return i;
}
}
lock.readLock().unlock();
lock.unlockRead();
return -1;
}

Expand Down

0 comments on commit 8b671be

Please sign in to comment.