001: /*
002: * @(#)DirClassMetaDataWriter.java
003: *
004: * Copyright (C) 2002-2004 Matt Albrecht
005: * groboclown@users.sourceforge.net
006: * http://groboutils.sourceforge.net
007: *
008: * Permission is hereby granted, free of charge, to any person obtaining a
009: * copy of this software and associated documentation files (the "Software"),
010: * to deal in the Software without restriction, including without limitation
011: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
012: * and/or sell copies of the Software, and to permit persons to whom the
013: * Software is furnished to do so, subject to the following conditions:
014: *
015: * The above copyright notice and this permission notice shall be included in
016: * all copies or substantial portions of the Software.
017: *
018: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
019: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
020: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
021: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
022: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
023: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
024: * DEALINGS IN THE SOFTWARE.
025: */
026:
027: package net.sourceforge.groboutils.codecoverage.v2.datastore;
028:
029: import java.io.IOException;
030:
031: import net.sourceforge.groboutils.codecoverage.v2.IAnalysisModule;
032:
033: /**
034: * Knows how to store the meta-data in a repository.
035: *
036: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
037: * @version $Date: 2004/04/15 05:48:26 $
038: * @since December 16, 2002
039: */
040: public class DirClassMetaDataWriter implements IClassMetaDataWriter {
041: private DirMetaDataIO store;
042: private IAnalysisModule am;
043: private AnalysisModuleSet ams;
044:
045: DirClassMetaDataWriter(IAnalysisModule module, DirMetaDataIO data)
046: throws IOException {
047: if (data == null || module == null) {
048: throw new IllegalArgumentException("no null args");
049: }
050: this .store = data;
051: this .am = module;
052: this .ams = data.getAnalysisModuleSet();
053: }
054:
055: /**
056: * Writes the given class record and its associated marks for this
057: * Analysis Module to the repository. It will overwrite any existing
058: * data.
059: */
060: public void writeClassRecord(ClassRecord cr) throws IOException {
061: if (cr == null) {
062: throw new IllegalArgumentException("no null args");
063: }
064: checkClose();
065:
066: // create the record for this particular module
067: cr = getAMClassRecord(cr);
068:
069: this .store.putClassRecord(this .am, cr);
070: }
071:
072: /**
073: * Closes off the writer's connection to the store. Mark writers may
074: * still have open connections that are still able to be used.
075: */
076: public void close() throws IOException {
077: checkClose();
078: // do not close the store!!!
079: this .store = null;
080: }
081:
082: /**
083: * Creates a class record for this specific module.
084: */
085: private ClassRecord getAMClassRecord(ClassRecord cr)
086: throws IOException {
087: ClassRecord c2 = new ClassRecord(cr.getClassName(), cr
088: .getClassCRC(), cr.getSourceFileName(),
089: cr.getMethods(), this .ams);
090:
091: // get only the marks for this analysis module
092: MarkRecord mr[] = cr.getMarksForAnalysisModule(this .am);
093: for (int i = 0; i < mr.length; ++i) {
094: // this guarantees a unique addition of marks
095: c2.addMark(mr[i]);
096: }
097:
098: return c2;
099: }
100:
101: private void checkClose() throws IOException {
102: if (this .store == null) {
103: throw new IOException("Writer has already been closed.");
104: }
105: }
106:
107: protected void finalize() throws Throwable {
108: super .finalize();
109:
110: // class-based post condition
111: if (this .store != null) {
112: throw new IllegalStateException("Did not close writer.");
113: }
114: }
115: }
|