001: package Schmortopf.FileStructure.DataStorage;
002:
003: /**
004: * Used in FileStructuresDataFile for holding
005: * one single Vector with FileStructureDescriptions
006: * and providing methods for loading and saving
007: * from/to a file.
008: */
009:
010: import java.util.*;
011: import java.io.*;
012:
013: import Schmortopf.Utility.Vectorizable;
014: import Schmortopf.FileStructure.Descriptions.*;
015:
016: public class FileStructureContainer implements Vectorizable {
017:
018: private String jarFilePath = "";
019: private String jarFileVersionIdentifier = "";
020:
021: private Vector fileStructureDescriptions = new Vector();
022:
023: /**
024: * Usual constructor :
025: * Creates the object and gets the versionidentifier from
026: * the jarfile with the passed filepath.
027: */
028: public FileStructureContainer(final String jarFilePath) {
029: this .jarFilePath = jarFilePath;
030: this .jarFileVersionIdentifier = FileStructuresDataFile
031: .CalculateVersionIdentifierFor(this .jarFilePath);
032: } // Constructor
033:
034: /**
035: * constructor 2:
036: * Creates the object and takes the passed versionIdentifier.
037: * This is used f.ex. for the projectfiles, where there actually
038: * is not one single jarfile, but many java textfiles.
039: * jarFilePath has no meaning in this case.
040: */
041: public FileStructureContainer(final String jarFilePath,
042: final String versionIdentifier) {
043: this .jarFilePath = jarFilePath;
044: this .jarFileVersionIdentifier = versionIdentifier;
045: } // Constructor
046:
047: /**
048: * Constructor for use, when the object will be recreated
049: * using createFromVectorRepresentation().
050: */
051: public FileStructureContainer() {
052: }
053:
054: /**
055: * Removes all FileStructureDescriptions,
056: * but doesn't release any other instances.
057: */
058: public void removeAllDescriptions() {
059: this .fileStructureDescriptions.setSize(0);
060: }
061:
062: public void addFileStructureDescription(
063: final FileStructureDescription fsd) {
064: this .fileStructureDescriptions.addElement(fsd);
065: }
066:
067: /**
068: * Returns the filename of the file, from which the
069: * hashtable data was created.
070: */
071: public String getFilePath() {
072: return this .jarFilePath;
073: }
074:
075: /**
076: * Returns the fileIdentifier of the file, from which the
077: * hashtable data was created, at the time when this was done.
078: */
079: public String getFileIdentifier() {
080: return this .jarFileVersionIdentifier;
081: }
082:
083: public FileStructureDescription[] getFileStructureDescriptions() {
084: final FileStructureDescription[] descriptions = new FileStructureDescription[this .fileStructureDescriptions
085: .size()];
086: this .fileStructureDescriptions.copyInto(descriptions);
087: return descriptions;
088: }
089:
090: public int getNumberOfFileStructureDescriptions() {
091: return this .fileStructureDescriptions.size();
092: }
093:
094: /**
095: * Gets the fsd at the passed index. No checks are made.
096: */
097: public FileStructureDescription getFSDAt(int index) {
098: return (FileStructureDescription) this .fileStructureDescriptions
099: .elementAt(index);
100: }
101:
102: /**
103: * returns the VectorRepresentation of the object's datacontent
104: */
105: public Vector getVectorRepresentation() throws Exception {
106: final Vector rep = new Vector();
107: rep.addElement(this .jarFilePath);
108: rep.addElement(this .jarFileVersionIdentifier);
109: // add the number of fsd's to follow :
110: rep.addElement(new Integer(this .fileStructureDescriptions
111: .size()));
112: // and add all fsd reps :
113: FileStructureDescription fsd = null;
114: for (int i = 0; i < this .fileStructureDescriptions.size(); i++) {
115: fsd = (FileStructureDescription) this .fileStructureDescriptions
116: .elementAt(i);
117: rep.addElement(fsd.getVectorRepresentation());
118: }
119: fsd = null;
120: return rep;
121: } // getVectorRepresentation
122:
123: /**
124: * Sets the object's content to the passed vectorRepresentation.
125: */
126: public void createFromVectorRepresentation(Vector rep) {
127: this .jarFilePath = (String) rep.elementAt(0);
128: this .jarFileVersionIdentifier = (String) rep.elementAt(1);
129: final int numberOfFSDReps = ((Integer) rep.elementAt(2))
130: .intValue();
131: Vector fsdRep;
132: FileStructureDescription fsd;
133: for (int i = 0; i < numberOfFSDReps; i++) {
134: fsdRep = (Vector) rep.elementAt(i + 3);
135: fsd = new FileStructureDescription();
136: fsd.createFromVectorRepresentation(fsdRep);
137: fsdRep.removeAllElements();
138: this .fileStructureDescriptions.addElement(fsd);
139: }
140: fsdRep = null;
141: fsd = null;
142: this .freeVector(rep);
143: } // createFromVectorRepresentation
144:
145: /**
146: * Recursive method, which removes any vector's in the passed vector.
147: */
148: private void freeVector(Vector v) {
149: Vector vHelp = null;
150: if (v != null) {
151: for (int i = 0; i < v.size(); i++) {
152: if (v.elementAt(i) instanceof Vector) {
153: vHelp = (Vector) v.elementAt(i);
154: this .freeVector(vHelp);
155: }
156: }
157: v.removeAllElements();
158: }
159: vHelp = null;
160: }
161:
162: /**
163: * This can be called before a container isnt used anymore.
164: * It helps the GC to remove not used memory.
165: */
166: public void removeAll() {
167: // Try to remove as much references *between* objects as possible,
168: // before they are left to the GC, and set the to null :
169: if (this .fileStructureDescriptions != null) {
170: this .fileStructureDescriptions.removeAllElements();
171: }
172: this .fileStructureDescriptions = null;
173: this .jarFilePath = null;
174: this .jarFileVersionIdentifier = null;
175: }
176:
177: } // FileStructureContainer
|