summaryrefslogtreecommitdiff
path: root/src/library/scala/collection/mutable/SynchronizedBuffer.scala
blob: 9c27f8b003f97c690b08a0a4b77a9e84bb4491f7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*                     __                                               *\
**     ________ ___   / /  ___     Scala API                            **
**    / __/ __// _ | / /  / _ |    (c) 2003-2013, LAMP/EPFL             **
**  __\ \/ /__/ __ |/ /__/ __ |                                         **
** /____/\___/_/ |_/____/_/ | |                                         **
**                          |/                                          **
\*                                                                      */



package scala
package collection
package mutable

import script._

/** This class should be used as a mixin. It synchronizes the `Buffer`
 *  methods of the class into which it is mixed in.
 *
 *  @tparam A    type of the elements contained in this buffer.
 *
 *  @author  Matthias Zenger
 *  @version 1.0, 08/07/2003
 *  @since   1
 *  @define Coll `SynchronizedBuffer`
 *  @define coll synchronized buffer
 */
@deprecated("Synchronization via traits is deprecated as it is inherently unreliable. Consider java.util.concurrent.ConcurrentLinkedQueue as an alternative.", "2.11.0")
trait SynchronizedBuffer[A] extends Buffer[A] {

  import scala.collection.Traversable

  abstract override def length: Int = synchronized {
    super.length
  }

  abstract override def iterator: Iterator[A] = synchronized {
    super.iterator
  }

  abstract override def apply(n: Int): A = synchronized {
    super.apply(n)
  }

  /** Append a single element to this buffer.
   *
   *  @param elem  the element to append.
   */
  abstract override def +=(elem: A): this.type = synchronized[this.type] {
    super.+=(elem)
  }

  /** Appends a number of elements provided by a traversable object via
   *  its `foreach` method.
   *  The identity of the buffer is returned.
   *
   *  @param xs the traversable object.
   */
  override def ++(xs: GenTraversableOnce[A]): Self = synchronized {
    super.++(xs)
  }

  /** Appends a number of elements provided by a traversable object
   *  via its `foreach` method.
   *
   *  @param xs   the iterable object.
   */
  override def ++=(xs: TraversableOnce[A]): this.type = synchronized[this.type] {
    super.++=(xs)
  }

  /** Appends a sequence of elements to this buffer.
   *
   *  @param elems  the elements to append.
   */
  override def append(elems: A*): Unit = synchronized {
    super.++=(elems)
  }

  /** Appends a number of elements provided by a traversable object
   *  via its `foreach` method.
   *
   *  @param xs the traversable object.
   */
  override def appendAll(xs: TraversableOnce[A]): Unit = synchronized {
    super.appendAll(xs)
  }

  /** Prepend a single element to this buffer and return
   *  the identity of the buffer.
   *
   *  @param elem  the element to append.
   */
  abstract override def +=:(elem: A): this.type = synchronized[this.type] {
    super.+=:(elem)
  }

  /** Prepends a number of elements provided by a traversable object
   *  via its `foreach` method. The identity of the buffer is returned.
   *
   *  @param xs the traversable object.
   */
  override def ++=:(xs: TraversableOnce[A]): this.type = synchronized[this.type] { super.++=:(xs) }

  /** Prepend an element to this list.
   *
   *  @param elems  the elements to prepend.
   */
  override def prepend(elems: A*): Unit = prependAll(elems)

  /** Prepends a number of elements provided by a traversable object
   *  via its `foreach` method. The identity of the buffer is returned.
   *
   *  @param xs the traversable object.
   */
  override def prependAll(xs: TraversableOnce[A]): Unit = synchronized {
    super.prependAll(xs)
  }

  /** Inserts new elements at the index `n`. Opposed to method `update`,
   *  this method will not replace an element with a one.
   *  Instead, it will insert the new elements at index `n`.
   *
   *  @param n      the index where a new element will be inserted.
   *  @param elems  the new elements to insert.
   */
  override def insert(n: Int, elems: A*): Unit = synchronized {
    super.insertAll(n, elems)
  }

  /** Inserts new elements at the index `n`. Opposed to method `update`,
   *  this method will not replace an element with a one.
   *  Instead, it will insert a new element at index `n`.
   *
   *  @param n     the index where a new element will be inserted.
   *  @param xs    the traversable object providing all elements to insert.
   */
  abstract override def insertAll(n: Int, xs: Traversable[A]): Unit = synchronized {
     super.insertAll(n, xs)
  }

  /** Replace element at index `n` with the new element `newelem`.
   *
   *  @param n       the index of the element to replace.
   *  @param newelem the new element.
   */
  abstract override def update(n: Int, newelem: A): Unit = synchronized {
    super.update(n, newelem)
  }

  /** Removes the element on a given index position.
   *
   *  @param n  the index which refers to the element to delete.
   */
  abstract override def remove(n: Int): A = synchronized {
    super.remove(n)
  }

  /** Clears the buffer contents.
   */
  abstract override def clear(): Unit = synchronized {
    super.clear()
  }

  @deprecated("scripting is deprecated", "2.11.0")
  override def <<(cmd: Message[A]): Unit = synchronized {
    super.<<(cmd)
  }

  /** Return a clone of this buffer.
   *
   *  @return an `ArrayBuffer` with the same elements.
   */
  override def clone(): Self = synchronized {
    super.clone()
  }

  /** The `hashCode` method always yields an error, since it is not
   *  safe to use buffers as keys in hash tables.
   *
   *  @return never.
   */
  override def hashCode(): Int = synchronized {
    super.hashCode()
  }
}