KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > user > server > rpc > impl > CharVector


1 /*
2  * Copyright 2007 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.user.server.rpc.impl;
17
18 /**
19  * A vector of primitive characters.
20  */

21 class CharVector {
22   private int capacityIncrement;
23   private char chars[];
24   private int size;
25
26   public CharVector(int initialCapacity, int capacityIncrement) {
27     assert (initialCapacity >= 0);
28     assert (capacityIncrement >= 0);
29
30     this.capacityIncrement = capacityIncrement;
31     chars = new char[initialCapacity];
32   }
33
34   public void add(char ch) {
35     if (size >= chars.length) {
36       int growBy = (capacityIncrement == 0) ? chars.length * 2
37           : capacityIncrement;
38       char newChars[] = new char[(chars.length + growBy)];
39       System.arraycopy(chars, 0, newChars, 0, size);
40       chars = newChars;
41     }
42
43     chars[size++] = ch;
44   }
45
46   public char[] asArray() {
47     return chars;
48   }
49
50   public char get(int index) {
51     assert (index < size);
52
53     return chars[index];
54   }
55
56   public int getSize() {
57     return size;
58   }
59
60   public void set(int index, char ch) {
61     assert (index >= 0 && index < size);
62
63     chars[index] = ch;
64     ++size;
65   }
66 }
Popular Tags