Skip to content

Commit 9ae9fba

Browse files
committed
Guava 13.0.1
1 parent 3c13eea commit 9ae9fba

12 files changed

Lines changed: 474 additions & 120 deletions

File tree

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,10 @@ Add it as a maven dependency:
5959
<dependency>
6060
<groupId>com.stackify</groupId>
6161
<artifactId>stackify-api-java</artifactId>
62-
<version>2.0.2</version>
62+
<version>2.0.3</version>
6363
</dependency>
6464
```
6565

66-
Note: *We are dependent on the Guava project from Google. We require version 14.0.1 (or beyond) for the background thread that sends data back to Stackify.*
67-
6866
## License
6967

7068
Copyright 2013 Stackify, LLC.

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
<dependency>
5252
<groupId>com.google.guava</groupId>
5353
<artifactId>guava</artifactId>
54-
<version>14.0.1</version>
54+
<version>13.0.1</version>
5555
</dependency>
5656

5757
<dependency>
Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
/*
2+
* Copyright 2015 Stackify
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.stackify.api.common.collect;
17+
18+
import java.util.ArrayDeque;
19+
import java.util.Collection;
20+
import java.util.Iterator;
21+
import java.util.Queue;
22+
23+
import com.google.common.base.Preconditions;
24+
25+
/**
26+
* SynchronizedEvictingQueue
27+
* @author Eric Martin
28+
*/
29+
public class SynchronizedEvictingQueue<E> implements Queue<E> {
30+
31+
/**
32+
* Maximum size of the queue
33+
*/
34+
private final int maxSize;
35+
36+
/**
37+
* Deque for the evicting queue implementation
38+
*/
39+
private final Queue<E> deque;
40+
41+
/**
42+
* Constructor
43+
* @param maxSize Maximum size of the queue
44+
*/
45+
public SynchronizedEvictingQueue(final int maxSize) {
46+
Preconditions.checkArgument(0 < maxSize);
47+
this.maxSize = maxSize;
48+
this.deque = new ArrayDeque<E>(maxSize);
49+
}
50+
51+
/**
52+
* @see java.util.Collection#size()
53+
*/
54+
@Override
55+
public synchronized int size() {
56+
return deque.size();
57+
}
58+
59+
/**
60+
* @see java.util.Collection#isEmpty()
61+
*/
62+
@Override
63+
public synchronized boolean isEmpty() {
64+
return deque.isEmpty();
65+
}
66+
67+
/**
68+
* @see java.util.Collection#contains(java.lang.Object)
69+
*/
70+
@Override
71+
public synchronized boolean contains(Object o) {
72+
return deque.contains(o);
73+
}
74+
75+
/**
76+
* @see java.util.Collection#iterator()
77+
*/
78+
@Override
79+
public synchronized Iterator<E> iterator() {
80+
return deque.iterator();
81+
}
82+
83+
/**
84+
* @see java.util.Collection#toArray()
85+
*/
86+
@Override
87+
public synchronized Object[] toArray() {
88+
return deque.toArray();
89+
}
90+
91+
/**
92+
* @see java.util.Collection#toArray(java.lang.Object[])
93+
*/
94+
@Override
95+
public synchronized <T> T[] toArray(T[] a) {
96+
return deque.toArray(a);
97+
}
98+
99+
/**
100+
* @see java.util.Collection#remove(java.lang.Object)
101+
*/
102+
@Override
103+
public synchronized boolean remove(Object o) {
104+
return deque.remove(o);
105+
}
106+
107+
/**
108+
* @see java.util.Collection#containsAll(java.util.Collection)
109+
*/
110+
@Override
111+
public synchronized boolean containsAll(Collection<?> c) {
112+
return deque.containsAll(c);
113+
}
114+
115+
/**
116+
* @see java.util.Collection#addAll(java.util.Collection)
117+
*/
118+
@Override
119+
public synchronized boolean addAll(Collection<? extends E> c) {
120+
Preconditions.checkNotNull(c);
121+
122+
for (E e : c) {
123+
add(e);
124+
}
125+
126+
return true;
127+
}
128+
129+
/**
130+
* @see java.util.Collection#removeAll(java.util.Collection)
131+
*/
132+
@Override
133+
public synchronized boolean removeAll(Collection<?> c) {
134+
return deque.removeAll(c);
135+
}
136+
137+
/**
138+
* @see java.util.Collection#retainAll(java.util.Collection)
139+
*/
140+
@Override
141+
public synchronized boolean retainAll(Collection<?> c) {
142+
return deque.retainAll(c);
143+
}
144+
145+
/**
146+
* @see java.util.Collection#clear()
147+
*/
148+
@Override
149+
public synchronized void clear() {
150+
deque.clear();
151+
}
152+
153+
/**
154+
* @see java.util.Queue#add(java.lang.Object)
155+
*/
156+
@Override
157+
public synchronized boolean add(E e) {
158+
Preconditions.checkNotNull(e);
159+
160+
if (deque.size() == maxSize) {
161+
deque.remove();
162+
}
163+
164+
deque.add(e);
165+
166+
return true;
167+
}
168+
169+
/**
170+
* @see java.util.Queue#offer(java.lang.Object)
171+
*/
172+
@Override
173+
public synchronized boolean offer(E e) {
174+
return add(e);
175+
}
176+
177+
/**
178+
* @see java.util.Queue#remove()
179+
*/
180+
@Override
181+
public synchronized E remove() {
182+
return deque.remove();
183+
}
184+
185+
/**
186+
* @see java.util.Queue#poll()
187+
*/
188+
@Override
189+
public synchronized E poll() {
190+
return deque.poll();
191+
}
192+
193+
/**
194+
* @see java.util.Queue#element()
195+
*/
196+
@Override
197+
public synchronized E element() {
198+
return deque.element();
199+
}
200+
201+
/**
202+
* @see java.util.Queue#peek()
203+
*/
204+
@Override
205+
public synchronized E peek() {
206+
return deque.peek();
207+
}
208+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2015 Stackify
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.stackify.api.common.concurrent;
17+
18+
import java.util.concurrent.ScheduledExecutorService;
19+
20+
import com.google.common.util.concurrent.AbstractScheduledService;
21+
import com.google.common.util.concurrent.MoreExecutors;
22+
23+
/**
24+
* BackgroundService
25+
* @author Eric Martin
26+
*/
27+
public abstract class BackgroundService extends AbstractScheduledService
28+
{
29+
/**
30+
* @see com.google.common.util.concurrent.AbstractScheduledService#executor()
31+
*/
32+
@Override
33+
protected ScheduledExecutorService executor() {
34+
35+
ScheduledExecutorService executor = super.executor();
36+
37+
// Add a listener to shutdown the executor after the service is stopped.
38+
// This is to work around a feature in Guava 13.0.1 that does not stop the executor when the service stops.
39+
40+
addListener(new ShutdownListener(executor), MoreExecutors.sameThreadExecutor());
41+
42+
return executor;
43+
}
44+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright 2015 Stackify
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.stackify.api.common.concurrent;
17+
18+
import java.util.concurrent.ScheduledExecutorService;
19+
20+
import com.google.common.base.Preconditions;
21+
import com.google.common.util.concurrent.Service.Listener;
22+
import com.google.common.util.concurrent.Service.State;
23+
24+
/**
25+
* ShutdownListener
26+
* @author Eric Martin
27+
*/
28+
public class ShutdownListener implements Listener {
29+
30+
/**
31+
* Executor
32+
*/
33+
private final ScheduledExecutorService executor;
34+
35+
/**
36+
* Constructor
37+
* @param executor Executor
38+
*/
39+
public ShutdownListener(final ScheduledExecutorService executor) {
40+
Preconditions.checkNotNull(executor);
41+
this.executor = executor;
42+
}
43+
44+
/**
45+
* @see com.google.common.util.concurrent.Service.Listener#starting()
46+
*/
47+
@Override
48+
public void starting() {
49+
}
50+
51+
/**
52+
* @see com.google.common.util.concurrent.Service.Listener#running()
53+
*/
54+
@Override
55+
public void running() {
56+
}
57+
58+
/**
59+
* @see com.google.common.util.concurrent.Service.Listener#stopping(com.google.common.util.concurrent.Service.State)
60+
*/
61+
@Override
62+
public void stopping(final State from) {
63+
}
64+
65+
/**
66+
* @see com.google.common.util.concurrent.Service.Listener#terminated(com.google.common.util.concurrent.Service.State)
67+
*/
68+
@Override
69+
public void terminated(final State from) {
70+
executor.shutdown();
71+
}
72+
73+
/**
74+
* @see com.google.common.util.concurrent.Service.Listener#failed(com.google.common.util.concurrent.Service.State, java.lang.Throwable)
75+
*/
76+
@Override
77+
public void failed(final State from, final Throwable failure) {
78+
executor.shutdown();
79+
}
80+
}

0 commit comments

Comments
 (0)