org/objectweb/proactive/branchnbound/core/Result.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
00028  * 
00029  * ################################################################
00030  */ 
00031 package org.objectweb.proactive.branchnbound.core;
00032 
00033 import java.io.IOException;
00034 import java.io.ObjectInputStream;
00035 import java.io.ObjectOutputStream;
00036 import java.io.Serializable;
00037 
00038 import org.objectweb.proactive.branchnbound.core.exception.NoResultsException;
00039 
00040 
00048 public class Result implements Serializable {
00049     private Object theSolution = null;
00050     private Exception exception = null;
00051 
00055     public Result() {
00056     }
00057 
00062     public Result(Object theSolution) {
00063         assert theSolution instanceof Comparable;
00064         this.theSolution = theSolution;
00065     }
00066 
00072     public Result(Exception e) {
00073         this.exception = e;
00074     }
00075 
00080     public Object getSolution() {
00081         return this.theSolution;
00082     }
00083 
00087     public Exception getException() {
00088         return this.exception;
00089     }
00090 
00095     public void setSolution(Object theSolution) {
00096         assert theSolution instanceof Comparable;
00097         this.theSolution = theSolution;
00098     }
00099 
00106     public Result returnTheBest(Result other) {
00107         if (((Comparable) this.theSolution).compareTo(other.getSolution()) <= 0) {
00108             return this;
00109         }
00110         return other;
00111     }
00112 
00116     public String toString() {
00117         return this.theSolution.toString();
00118     }
00119 
00125     public boolean isBetterThan(Result other) {
00126         if (((Comparable) this.theSolution).compareTo(other.theSolution) < 0) {
00127             return true;
00128         }
00129         return false;
00130     }
00131 
00135     public boolean isAnException() {
00136         return this.exception != null;
00137     }
00138 
00139     // Serialization ----------------------------------------------------------
00140     private static final String NORESULT = "--No result--";
00141 
00142     private void writeObject(ObjectOutputStream out) throws IOException {
00143         if (this.theSolution instanceof NoResultsException) {
00144             out.write(NORESULT.getBytes());
00145         } else {
00146             out.writeObject(this.theSolution);
00147         }
00148     }
00149 
00150     private void readObject(ObjectInputStream in)
00151         throws IOException, ClassNotFoundException {
00152         Object readObject = in.readObject();
00153         if (readObject instanceof String &&
00154                 (((String) readObject).compareTo(NORESULT) == 0)) {
00155             this.theSolution = new NoResultsException();
00156         } else {
00157             this.theSolution = readObject;
00158         }
00159     }
00160 }

Generated on Mon Jan 22 15:16:05 2007 for ProActive by  doxygen 1.5.1