001/* DoubleBuffer.java -- 
002   Copyright (C) 2002, 2003, 2004, 2005  Free Software Foundation, Inc.
003
004This file is part of GNU Classpath.
005
006GNU Classpath is free software; you can redistribute it and/or modify
007it under the terms of the GNU General Public License as published by
008the Free Software Foundation; either version 2, or (at your option)
009any later version.
010
011GNU Classpath is distributed in the hope that it will be useful, but
012WITHOUT ANY WARRANTY; without even the implied warranty of
013MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014General Public License for more details.
015
016You should have received a copy of the GNU General Public License
017along with GNU Classpath; see the file COPYING.  If not, write to the
018Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
01902110-1301 USA.
020
021Linking this library statically or dynamically with other modules is
022making a combined work based on this library.  Thus, the terms and
023conditions of the GNU General Public License cover the whole
024combination.
025
026As a special exception, the copyright holders of this library give you
027permission to link this library with independent modules to produce an
028executable, regardless of the license terms of these independent
029modules, and to copy and distribute the resulting executable under
030terms of your choice, provided that you also meet, for each linked
031independent module, the terms and conditions of the license of that
032module.  An independent module is a module which is not derived from
033or based on this library.  If you modify this library, you may extend
034this exception to your version of the library, but you are not
035obligated to do so.  If you do not wish to do so, delete this
036exception statement from your version. */
037
038
039package java.nio;
040
041// GCJ LOCAL: Change gnu.classpath.Pointer to RawData
042import gnu.gcj.RawData;
043
044/**
045 * @since 1.4
046 */
047public abstract class DoubleBuffer extends Buffer
048  implements Comparable<DoubleBuffer>
049{
050  final int array_offset;
051  final double[] backing_buffer;
052
053  DoubleBuffer (int capacity, int limit, int position, int mark,
054                RawData address, double[] backing_buffer, int array_offset)
055  {
056    super (capacity, limit, position, mark, address);
057    this.backing_buffer = backing_buffer;
058    this.array_offset = array_offset;
059  }
060
061  /**
062   * Allocates a new <code>DoubleBuffer</code> object with a given capacity.
063   */
064  public static DoubleBuffer allocate (int capacity)
065  {
066    return new DoubleBufferImpl (capacity);
067  }
068
069  /**
070   * Wraps a <code>double</code> array into a <code>DoubleBuffer</code>
071   * object.
072   *
073   * @exception IndexOutOfBoundsException If the preconditions on the offset
074   * and length parameters do not hold
075   */
076  public static final DoubleBuffer wrap (double[] array, int offset, int length)
077  {
078    return new DoubleBufferImpl (array, 0, array.length, offset + length, offset, -1, false);
079  }
080
081  /**
082   * Wraps a <code>double</code> array into a <code>DoubleBuffer</code>
083   * object.
084   */
085  public static final DoubleBuffer wrap (double[] array)
086  {
087    return wrap (array, 0, array.length);
088  }
089  
090  /**
091   * This method transfers <code>double</code>s from this buffer into the given
092   * destination array. Before the transfer, it checks if there are fewer than
093   * length <code>double</code>s remaining in this buffer. 
094   *
095   * @param dst The destination array
096   * @param offset The offset within the array of the first <code>double</code>
097   * to be written; must be non-negative and no larger than dst.length.
098   * @param length The maximum number of bytes to be written to the given array;
099   * must be non-negative and no larger than dst.length - offset.
100   *
101   * @exception BufferUnderflowException If there are fewer than length
102   * <code>double</code>s remaining in this buffer.
103   * @exception IndexOutOfBoundsException If the preconditions on the offset
104   * and length parameters do not hold.
105   */
106  public DoubleBuffer get (double[] dst, int offset, int length)
107  {
108    checkArraySize(dst.length, offset, length);
109    checkForUnderflow(length);
110
111    for (int i = offset; i < offset + length; i++)
112      {
113        dst [i] = get ();
114      }
115
116    return this;
117  }
118
119  /**
120   * This method transfers <code>double</code>s from this buffer into the given
121   * destination array.
122   *
123   * @param dst The byte array to write into.
124   *
125   * @exception BufferUnderflowException If there are fewer than dst.length
126   * <code>double</code>s remaining in this buffer.
127   */
128  public DoubleBuffer get (double[] dst)
129  {
130    return get (dst, 0, dst.length);
131  }
132
133  /**
134   * Writes the content of the the <code>DoubleBUFFER</code> src
135   * into the buffer. Before the transfer, it checks if there is fewer than
136   * <code>src.remaining()</code> space remaining in this buffer.
137   *
138   * @param src The source data.
139   *
140   * @exception BufferOverflowException If there is insufficient space in this
141   * buffer for the remaining <code>double</code>s in the source buffer.
142   * @exception IllegalArgumentException If the source buffer is this buffer.
143   * @exception ReadOnlyBufferException If this buffer is read-only.
144   */
145  public DoubleBuffer put (DoubleBuffer src)
146  {
147    if (src == this)
148      throw new IllegalArgumentException ();
149
150    checkForOverflow(src.remaining ());
151
152    if (src.remaining () > 0)
153      {
154        double[] toPut = new double [src.remaining ()];
155        src.get (toPut);
156        put (toPut);
157      }
158
159    return this;
160  }
161
162  /**
163   * Writes the content of the the <code>double array</code> src
164   * into the buffer. Before the transfer, it checks if there is fewer than
165   * length space remaining in this buffer.
166   *
167   * @param src The array to copy into the buffer.
168   * @param offset The offset within the array of the first byte to be read;
169   * must be non-negative and no larger than src.length.
170   * @param length The number of bytes to be read from the given array;
171   * must be non-negative and no larger than src.length - offset.
172   * 
173   * @exception BufferOverflowException If there is insufficient space in this
174   * buffer for the remaining <code>double</code>s in the source array.
175   * @exception IndexOutOfBoundsException If the preconditions on the offset
176   * and length parameters do not hold
177   * @exception ReadOnlyBufferException If this buffer is read-only.
178   */
179  public DoubleBuffer put (double[] src, int offset, int length)
180  {
181    checkArraySize(src.length, offset, length);
182    checkForOverflow(length);
183
184    for (int i = offset; i < offset + length; i++)
185      put (src [i]);
186
187    return this;
188  }
189
190  /**
191   * Writes the content of the the <code>double array</code> src
192   * into the buffer.
193   *
194   * @param src The array to copy into the buffer.
195   * 
196   * @exception BufferOverflowException If there is insufficient space in this
197   * buffer for the remaining <code>double</code>s in the source array.
198   * @exception ReadOnlyBufferException If this buffer is read-only.
199   */
200  public final DoubleBuffer put (double[] src)
201  {
202    return put (src, 0, src.length);
203  }
204
205  /**
206   * Tells whether ot not this buffer is backed by an accessible
207   * <code>double</code> array.
208   */
209  public final boolean hasArray ()
210  {
211    return (backing_buffer != null
212            && !isReadOnly ());
213  }
214
215  /**
216   * Returns the <code>double</code> array that backs this buffer.
217   *
218   * @exception ReadOnlyBufferException If this buffer is read-only.
219   * @exception UnsupportedOperationException If this buffer is not backed
220   * by an accessible array.
221   */
222  public final double[] array ()
223  {
224    if (backing_buffer == null)
225      throw new UnsupportedOperationException ();
226
227    checkIfReadOnly();
228    
229    return backing_buffer;
230  }
231
232  /**
233   * Returns the offset within this buffer's backing array of the first element.
234   *
235   * @exception ReadOnlyBufferException If this buffer is read-only.
236   * @exception UnsupportedOperationException If this buffer is not backed
237   * by an accessible array.
238   */
239  public final int arrayOffset ()
240  {
241    if (backing_buffer == null)
242      throw new UnsupportedOperationException ();
243
244    checkIfReadOnly();
245    
246    return array_offset;
247  }
248
249  /**
250   * Calculates a hash code for this buffer.
251   *
252   * This is done with <code>long</code> arithmetic,
253   * where ** represents exponentiation, by this formula:<br>
254   * <code>s[position()] + 31 + (s[position()+1] + 30)*31**1 + ... +
255   * (s[limit()-1]+30)*31**(limit()-1)</code>.
256   * Where s is the buffer data, in Double.doubleToLongBits() form
257   * Note that the hashcode is dependent on buffer content, 
258   * and therefore is not useful if the buffer content may change.
259   *
260   * @return the hash code (casted to int)
261   */
262  public int hashCode ()
263  {
264    long hashCode = Double.doubleToLongBits(get(position())) + 31;
265    long multiplier = 1;
266    for (int i = position() + 1; i < limit(); ++i)
267      {
268          multiplier *= 31;
269          hashCode += (Double.doubleToLongBits(get(i)) + 30)*multiplier;
270      }
271    return ((int)hashCode);
272  }
273
274  /**
275   * Checks if this buffer is equal to obj.
276   */
277  public boolean equals (Object obj)
278  {
279    if (obj instanceof DoubleBuffer)
280      {
281        return compareTo ((DoubleBuffer) obj) == 0;
282      }
283
284    return false;
285  }
286
287  /**
288   * Compares two <code>DoubleBuffer</code> objects.
289   *
290   * @exception ClassCastException If obj is not an object derived from
291   * <code>DoubleBuffer</code>.
292   */
293  public int compareTo (DoubleBuffer other)
294  {
295    int num = Math.min(remaining(), other.remaining());
296    int pos_this = position();
297    int pos_other = other.position();
298    
299    for (int count = 0; count < num; count++)
300      {
301        double a = get(pos_this++);
302        double b = other.get(pos_other++);
303         
304        if (a == b)
305          continue;
306           
307        if (a < b)
308          return -1;
309           
310        return 1;
311      }
312      
313    return remaining() - other.remaining();
314  }
315
316  /**
317   * Returns the byte order of this buffer.
318   */
319  public abstract ByteOrder order ();
320
321  /**
322   * Reads the <code>double</code> at this buffer's current position,
323   * and then increments the position.
324   *
325   * @exception BufferUnderflowException If there are no remaining
326   * <code>double</code>s in this buffer.
327   */
328  public abstract double get ();
329
330  /**
331   * Writes the <code>double</code> at this buffer's current position,
332   * and then increments the position.
333   *
334   * @exception BufferOverflowException If there no remaining 
335   * <code>double</code>s in this buffer.
336   * @exception ReadOnlyBufferException If this buffer is read-only.
337   */
338  public abstract DoubleBuffer put (double b);
339
340  /**
341   * Absolute get method.
342   *
343   * @exception IndexOutOfBoundsException If index is negative or not smaller
344   * than the buffer's limit.
345   */
346  public abstract double get (int index);
347  
348  /**
349   * Absolute put method.
350   *
351   * @exception IndexOutOfBoundsException If index is negative or not smaller
352   * than the buffer's limit.
353   * @exception ReadOnlyBufferException If this buffer is read-only.
354   */
355  public abstract DoubleBuffer put (int index, double b);
356
357  /**
358   * Compacts this buffer.
359   * 
360   * @exception ReadOnlyBufferException If this buffer is read-only.
361   */
362  public abstract DoubleBuffer compact ();
363
364  /**
365   * Tells wether or not this buffer is direct.
366   */
367  public abstract boolean isDirect ();
368
369  /**
370   * Creates a new <code>DoubleBuffer</code> whose content is a shared
371   * subsequence of this buffer's content.
372   */
373  public abstract DoubleBuffer slice ();
374
375  /**
376   * Creates a new <code>DoubleBuffer</code> that shares this buffer's
377   * content.
378   */
379  public abstract DoubleBuffer duplicate ();
380
381  /**
382   * Creates a new read-only <code>DoubleBuffer</code> that shares this
383   * buffer's content.
384   */
385  public abstract DoubleBuffer asReadOnlyBuffer ();
386}