1 /*
2 * ModeShape (http://www.modeshape.org)
3 * See the COPYRIGHT.txt file distributed with this work for information
4 * regarding copyright ownership. Some portions may be licensed
5 * to Red Hat, Inc. under one or more contributor license agreements.
6 * See the AUTHORS.txt file in the distribution for a full listing of
7 * individual contributors.
8 *
9 * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10 * is licensed to you under the terms of the GNU Lesser General Public License as
11 * published by the Free Software Foundation; either version 2.1 of
12 * the License, or (at your option) any later version.
13 *
14 * ModeShape is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this software; if not, write to the Free
21 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23 */
24 package org.modeshape.common.collection;
25
26 import java.util.ArrayList;
27 import java.util.Collections;
28 import java.util.LinkedList;
29 import java.util.List;
30 import java.util.concurrent.locks.ReadWriteLock;
31 import java.util.concurrent.locks.ReentrantReadWriteLock;
32 import net.jcip.annotations.ThreadSafe;
33
34 /**
35 * A thread-safe {@link Problems} collection. The problems will be {@link #iterator() returned} in the order in which they were
36 * encountered.
37 */
38 @ThreadSafe
39 public class ThreadSafeProblems extends AbstractProblems {
40 private static final long serialVersionUID = 1L;
41
42 private final ReadWriteLock lock = new ReentrantReadWriteLock();
43 private final List<Problem> problems = new LinkedList<Problem>();
44
45 /**
46 * {@inheritDoc}
47 *
48 * @see org.modeshape.common.collection.AbstractProblems#hasErrors()
49 */
50 @Override
51 public boolean hasErrors() {
52 try {
53 lock.readLock().lock();
54 return super.hasErrors();
55 } finally {
56 lock.readLock().unlock();
57 }
58 }
59
60 /**
61 * {@inheritDoc}
62 *
63 * @see org.modeshape.common.collection.AbstractProblems#hasProblems()
64 */
65 @Override
66 public boolean hasProblems() {
67 try {
68 lock.readLock().lock();
69 return super.hasProblems();
70 } finally {
71 lock.readLock().unlock();
72 }
73 }
74
75 /**
76 * {@inheritDoc}
77 *
78 * @see org.modeshape.common.collection.AbstractProblems#hasInfo()
79 */
80 @Override
81 public boolean hasInfo() {
82 try {
83 lock.readLock().lock();
84 return super.hasInfo();
85 } finally {
86 lock.readLock().unlock();
87 }
88 }
89
90 /**
91 * {@inheritDoc}
92 *
93 * @see org.modeshape.common.collection.AbstractProblems#hasWarnings()
94 */
95 @Override
96 public boolean hasWarnings() {
97 try {
98 lock.readLock().lock();
99 return super.hasWarnings();
100 } finally {
101 lock.readLock().unlock();
102 }
103 }
104
105 /**
106 * {@inheritDoc}
107 *
108 * @see org.modeshape.common.collection.AbstractProblems#isEmpty()
109 */
110 @Override
111 public boolean isEmpty() {
112 try {
113 lock.readLock().lock();
114 return super.isEmpty();
115 } finally {
116 lock.readLock().unlock();
117 }
118 }
119
120 /**
121 * {@inheritDoc}
122 *
123 * @see org.modeshape.common.collection.AbstractProblems#size()
124 */
125 @Override
126 public int size() {
127 try {
128 lock.readLock().lock();
129 return super.size();
130 } finally {
131 lock.readLock().unlock();
132 }
133 }
134
135 /**
136 * {@inheritDoc}
137 *
138 * @see org.modeshape.common.collection.AbstractProblems#addProblem(Problem)
139 */
140 @Override
141 protected void addProblem( Problem problem ) {
142 try {
143 lock.writeLock().lock();
144 problems.add(problem);
145 } finally {
146 lock.writeLock().unlock();
147 }
148 }
149
150 /**
151 * {@inheritDoc}
152 *
153 * @see org.modeshape.common.collection.AbstractProblems#getProblems()
154 */
155 @Override
156 protected List<Problem> getProblems() {
157 // Return an unmodifiable copy ...
158 try {
159 lock.readLock().lock();
160 return Collections.unmodifiableList(new ArrayList<Problem>(this.problems));
161 } finally {
162 lock.readLock().unlock();
163 }
164 }
165 }