home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-18 | 8.3 KB | 274 lines |
- /*
- * Copyright (c) 1997 Krumel & Associates, Inc. All Rights Reserved.
- *
- * www.krumel.com - controls@krumel.com
- *
- * Permission is given to the buyer of this package for one software
- * developer to use this software on one CPU (one workstation) and to make
- * one backup copy. You may uitilize and/or modify this class for use in your
- * projects. You may distribute or sell any executable which results from
- * using this code in yur application, except a utility or class of similar
- * nature to this product. You may distribute this product in compiled
- * form only, but soley to be used with your cmpiled executable product
- * for the puposes of dynamic loading. You may NOT redistribute the source
- * code in any form or make it accessible through a network or other
- * distribution media to others. Please refer to the file "copyright.html"
- * for further important copyright and licensing information.
- *
- * The source code is the confidential and proprietary information
- * of Krumel & Associates, Inc. ("Confidential Information"). You shall
- * not disclose such Confidential Information and shall use it only in
- * accordance with the terms of the license agreement you entered into
- * with Krumel & Associates, Inc..
-
- * KRUMEL & ASSOCIATES MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE
- * SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT
- * NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
- * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. KRUMEL & ASSOCIATES SHALL NOT
- * BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
- * MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
- */
-
- package symantec.itools.db.awt;
-
- /**
- * Class represents the possible states for a piece of data.
- */
- public class DataState {
- int state = NEW;
-
- DataState link;
- /**
- * Data has been created and not updated or saved.
- */
- public static final int NEW = 0;
- /**
- * Data has been previously saved and not modified.
- */
- public static final int CLEAN = 1;
- /**
- * Base value for having been modified.
- */
- static final int MODIFIED = 2;
- /**
- * Data has not been previously saved but has been modified since creation.
- */
- public static final int NEW_MODIFIED = 2;
- /**
- * Data has been previously saved and modified.
- */
- public static final int CLEAN_MODIFIED = 3;
- /**
- * Base value for deletion.
- */
- static final int DELETED = 4;
- /**
- * Data has not been previously saved or modified, but has been marked for deletion.
- */
- public static final int NEW_DELETED = 4;
- /**
- * Data has been previously saved and marked for deletion, but not modified.
- */
- public static final int CLEAN_DELETED = 5;
- /**
- * Data has not been previously saved, but has been modified and marked for deletion.
- */
- public static final int NEW_MOD_DELETED = 6;
- /**
- * Data has been previously save, changed, and marked for deletion.
- */
- public static final int CLEAN_MOD_DELETED = 7;
-
-
- static final int MASK = (1 << 31)-1;
-
- /**
- * Constructs a state of NEW.
- */
- public DataState() {}
-
- /**
- * Creates a state of either NEW or CLEAN.
- */
- public DataState(int initialState) {
- state = (initialState == NEW) ? NEW :CLEAN;
- }
-
- /**
- * Gets whether the specified state is a delete state.
- */
- public static boolean deleteState(int state) {
- return (state & DELETED) != 0;
- }
-
- /**
- * Gets whether the object has been marked for deletion.
- */
- public boolean deleted() {
- return (state & DELETED) != 0;
- }
-
- /**
- * Gets whether the object has been modified since creation or the last save.
- */
- public boolean modified() {
- return (state & MODIFIED) != 0;
- }
-
- /**
- * Gets whether the object has been modified since creation or the last save.
- */
- public boolean isModified() {
- return (state & MODIFIED) != 0;
- }
-
- /**
- * Gets whether the object has not been previously saved.
- */
- public boolean isNew() {
- return (state & 1) == 0;
- }
-
- /**
- * Gets whether the object has not been changed since last save.
- */
- public boolean isClean() {
- return state == CLEAN;
- }
-
- /**
- * Gets whether the object has been marked for deletion.
- */
- public boolean isDeleted() {
- return (state & DELETED) != 0;
- }
-
- /**
- * Gets whether the object has been modified or or marked for deletion.
- */
- public boolean changedState() {
- return deleted() || modified();
- }
-
- /**
- * Links this object to the state of another object. If this object's state is modified
- * the link's state will be modified accordingly.
- */
- public void link(DataState l) { link = l; }
-
- /**
- * Gets the state value.
- */
- public int state() { return state; }
-
- /**
- * Marks the object as new object. This method is a noop and is included for completeness.
- * Objects can only be marked as new when they are created.
- */
- public void markNew() {
- //can only mark new if already new - this method is a noop
- }
-
- /**
- * Marks the object as ready for deletion.
- */
- public void markDeleted() {
- state = DELETED | state;
- if (link != null) {
- link.markDeleted();
- }
- }
-
- /**
- * Resets the state of the object to the state before it was marked for deletion.
- */
- public void markUndelete() {
- state &= (DELETED ^ MASK);
- if (link != null) {
- link.markUndelete();
- }
- }
-
- /**
- * Sets the state to clean (has been saved and not modified).
- */
- public void markClean() {
- state = state | (CLEAN ^ (state>>2));
- if (link != null) {
- link.markClean();
- }
- }
-
- /**
- * Marks the object as having been modified.
- */
- public void markModified() {
- state = state | MODIFIED;
- if (link != null) {
- link.markModified();
- }
-
- }
-
- /**
- * Marks this object as CLEAN but does not make a change in the state link. This
- * is handy when sub objects are being saved before the owner object is saved.
- */
- public void updated() {
- //do not want to update link b/c some action could still depend on
- //master link's state
- state = CLEAN;
- }
-
- /**
- * Prints the state information to the standard output stream.
- */
- public void printState() {
- switch(state) {
- case NEW: System.out.println("state=NEW"); break;
- case CLEAN: System.out.println("state=CLEAN"); break;
- case NEW_MODIFIED: System.out.println("state=NEW_MODIFIED"); break;
- case CLEAN_MODIFIED: System.out.println("state=CLEAN_MODIFIED"); break;
- case NEW_DELETED: System.out.println("state=NEW_DELETED"); break;
- case CLEAN_DELETED: System.out.println("state=CLEAN_DELETED"); break;
- case NEW_MOD_DELETED:
- System.out.println("state=NEW_MOD_DELETED");
- break;
- case CLEAN_MOD_DELETED:
- System.out.println("state=CLEAN_MOD_DELETED");
- break;
- default: System.out.println("in illegal state - " + state);
- }
- }
-
- /*
- public static void main(String a[]) throws Exception {
- DataState ds = new DataState();
- ds.printState();
- System.out.println(ds.isNew());
- ds.markModified();
- System.out.println(ds.isNew());
- ds.printState();
- ds.markDeleted();
- System.out.println(ds.isNew());
- ds.printState();
- ds.markModified();
- ds.printState();
- ds.markUndelete();
- ds.printState();
- ds.markClean();
- ds.printState();
- ds.updated();
- System.out.println(ds.isNew());
- ds.printState();
- ds.markModified();
- ds.printState();
- ds.markDeleted();
- ds.printState();
- ds.markUndelete();
- ds.printState();
-
- System.in.read();
- }
- */
- }