001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.vfs.provider.ram;
018:
019: import java.io.Serializable;
020: import java.util.ArrayList;
021: import java.util.Collection;
022: import java.util.Collections;
023:
024: import org.apache.commons.vfs.FileName;
025: import org.apache.commons.vfs.FileSystemException;
026: import org.apache.commons.vfs.FileType;
027:
028: /**
029: * RAM File Object Data
030: */
031: class RamFileData implements Serializable {
032: /**
033: * File Name
034: */
035: private FileName name;
036:
037: /**
038: * File Type
039: */
040: private FileType type;
041:
042: /**
043: * Bytes
044: */
045: private byte[] buffer;
046:
047: /**
048: * Last modified time
049: */
050: private long lastModified;
051:
052: /**
053: * Children
054: */
055: private Collection children;
056:
057: /**
058: * Constructor
059: */
060: public RamFileData(FileName name) {
061: super ();
062: this .clear();
063: if (name == null) {
064: throw new IllegalArgumentException("name can not be null");
065: }
066: this .name = name;
067: }
068:
069: /**
070: * @return Returns the buffer.
071: */
072: byte[] getBuffer() {
073: return buffer;
074: }
075:
076: /**
077: * @param buffer
078: */
079: void setBuffer(byte[] buffer) {
080: updateLastModified();
081: this .buffer = buffer;
082: }
083:
084: /**
085: * @return Returns the lastModified.
086: */
087: long getLastModified() {
088: return lastModified;
089: }
090:
091: /**
092: * @param lastModified
093: * The lastModified to set.
094: */
095: void setLastModified(long lastModified) {
096: this .lastModified = lastModified;
097: }
098:
099: /**
100: * @return Returns the type.
101: */
102: FileType getType() {
103: return type;
104: }
105:
106: /**
107: * @param type
108: * The type to set.
109: */
110: void setType(FileType type) {
111: this .type = type;
112: }
113:
114: /**
115: *
116: */
117: void clear() {
118: this .buffer = new byte[0];
119: updateLastModified();
120: this .type = FileType.IMAGINARY;
121: this .children = Collections
122: .synchronizedCollection(new ArrayList());
123: this .name = null;
124: }
125:
126: void updateLastModified() {
127: this .lastModified = System.currentTimeMillis();
128: }
129:
130: /**
131: * @return Returns the name.
132: */
133: FileName getName() {
134: return name;
135: }
136:
137: /*
138: * (non-Javadoc)
139: *
140: * @see java.lang.Object#toString()
141: */
142: public String toString() {
143: return this .name.toString();
144: }
145:
146: /**
147: * Add a child
148: *
149: * @param data
150: */
151: void addChild(RamFileData data) throws FileSystemException {
152: if (!this .getType().hasChildren()) {
153: throw new FileSystemException(
154: "A child can only be added in a folder");
155: }
156:
157: if (data == null) {
158: throw new FileSystemException("No child can be null");
159: }
160:
161: if (this .children.contains(data)) {
162: throw new FileSystemException("Child already exists. "
163: + data);
164: }
165:
166: this .children.add(data);
167: updateLastModified();
168: }
169:
170: /**
171: * Remove a child
172: *
173: * @param data
174: * @throws FileSystemException
175: */
176: void removeChild(RamFileData data) throws FileSystemException {
177: if (!this .getType().hasChildren()) {
178: throw new FileSystemException(
179: "A child can only be removed from a folder");
180: }
181: if (!this .children.contains(data)) {
182: throw new FileSystemException("Child not found. " + data);
183: }
184: this .children.remove(data);
185: updateLastModified();
186: }
187:
188: /**
189: * @return Returns the children.
190: */
191: Collection getChildren() {
192: if (name == null) {
193: throw new IllegalStateException("Data is clear");
194: }
195: return children;
196: }
197:
198: /*
199: * (non-Javadoc)
200: *
201: * @see java.lang.Object#equals(java.lang.Object)
202: */
203: public boolean equals(Object o) {
204: RamFileData data = (RamFileData) o;
205: return this .getName().equals(data.getName());
206: }
207:
208: /*
209: * (non-Javadoc)
210: *
211: * @see java.lang.Object#hashCode()
212: */
213: public int hashCode() {
214: return this .getName().hashCode();
215: }
216:
217: boolean hasChildren(RamFileData data) {
218: return this .children.contains(data);
219: }
220:
221: /**
222: * @return Returns the size of the buffer
223: */
224: int size() {
225: return buffer.length;
226: }
227:
228: /**
229: * Resize the buffer
230: *
231: * @param newSize
232: */
233: void resize(int newSize) {
234: int size = this .size();
235: byte[] newBuf = new byte[newSize];
236: System.arraycopy(this .buffer, 0, newBuf, 0, size);
237: this.buffer = newBuf;
238: updateLastModified();
239: }
240:
241: }
|