Commit 8d920b8e authored by Luc Maisonobe's avatar Luc Maisonobe

Fixed findbugs warning.

parent 571974f9
......@@ -42,6 +42,7 @@
<Class name="~.*\.FootprintOverlapDetector$" />
<Class name="~.*\.GeographicZoneDetector$" />
<Class name="~.*\.SaastamoinenModel$" />
<Class name="~.*\.EventEnablingPredicateFilter$" />
</Or>
<Bug pattern="SE_TRANSIENT_FIELD_NOT_RESTORED" />
</Match>
......
......@@ -16,6 +16,8 @@
*/
package org.orekit.propagation.events;
import java.io.NotSerializableException;
import java.io.Serializable;
import java.util.Arrays;
import org.orekit.errors.OrekitException;
......@@ -69,22 +71,22 @@ public class EventEnablingPredicateFilter<T extends EventDetector>
private final T rawDetector;
/** Enabling predicate function. */
private final EnablingPredicate<T> enabler;
private final transient EnablingPredicate<T> enabler;
/** Transformers of the g function. */
private final Transformer[] transformers;
private final transient Transformer[] transformers;
/** Update time of the transformers. */
private final AbsoluteDate[] updates;
private final transient AbsoluteDate[] updates;
/** Indicator for forward integration. */
private boolean forward;
private transient boolean forward;
/** Extreme time encountered so far. */
private AbsoluteDate extremeT;
private transient AbsoluteDate extremeT;
/** Detector function value at extremeT. */
private double extremeG;
private transient double extremeG;
/** Wrap an {@link EventDetector event detector}.
* @param rawDetector event detector to wrap
......@@ -274,6 +276,19 @@ public class EventEnablingPredicateFilter<T extends EventDetector>
}
}
/** Replace the instance with a data transfer object for serialization.
* @return data transfer object that will be serialized
* @exception NotSerializableException if the {@link EnablingPredicate
* enabling predicate} is not serializable
*/
private Object writeReplace() throws NotSerializableException {
if (enabler instanceof Serializable) {
return new DataTransferObject(rawDetector, (Serializable) enabler);
} else {
throw new NotSerializableException(enabler.getClass().getName());
}
}
/** Local handler. */
private static class LocalHandler<T extends EventDetector> implements EventHandler<EventEnablingPredicateFilter<T>> {
......@@ -293,4 +308,36 @@ public class EventEnablingPredicateFilter<T extends EventDetector>
}
/** Internal class used only for serialization. */
private static class DataTransferObject implements Serializable {
/** Serializable UID. */
private static final long serialVersionUID = 20160321L;
/** Wrapped event detector. */
private final EventDetector rawDetector;
/** Enabling predicate function. */
private final Serializable enabler;
/** Simple constructor.
* @param rawDetector wrapped event detector
* @param enabler enabling predicate function
*/
DataTransferObject(final EventDetector rawDetector, final Serializable enabler) {
this.rawDetector = rawDetector;
this.enabler = enabler;
}
/** Replace the deserialized data transfer object with a {@link EventEnablingPredicateFilter}.
* @return replacement {@link EventEnablingPredicateFilter}
*/
@SuppressWarnings("unchecked")
private Object readResolve() {
return new EventEnablingPredicateFilter<EventDetector>(rawDetector,
(EnablingPredicate<EventDetector>) enabler);
}
}
}
......@@ -16,6 +16,8 @@
*/
package org.orekit.propagation.events.handlers;
import java.io.Serializable;
import org.orekit.errors.OrekitException;
import org.orekit.propagation.SpacecraftState;
import org.orekit.propagation.events.EventDetector;
......@@ -28,7 +30,10 @@ import org.orekit.propagation.events.EventDetector;
* @param <T> class type for the generic version
* @since 6.1
*/
public class ContinueOnEvent<T extends EventDetector> implements EventHandler<T> {
public class ContinueOnEvent<T extends EventDetector> implements EventHandler<T>, Serializable {
/** Serializable UID. */
private static final long serialVersionUID = 20160321L;
/**
* Specific implementation of the eventOccurred interface.
......
......@@ -16,6 +16,8 @@
*/
package org.orekit.propagation.events.handlers;
import java.io.Serializable;
import org.orekit.errors.OrekitException;
import org.orekit.propagation.SpacecraftState;
import org.orekit.propagation.events.EventDetector;
......@@ -31,7 +33,10 @@ import org.orekit.propagation.events.EventDetector;
* @param <T> class type for the generic version
* @since 6.1
*/
public class StopOnDecreasing <T extends EventDetector> implements EventHandler<T> {
public class StopOnDecreasing <T extends EventDetector> implements EventHandler<T>, Serializable {
/** Serializable UID. */
private static final long serialVersionUID = 20160321L;
/** Handle a detection event and choose what to do next.
* <p>The implementation behavior is to {@link
......
......@@ -16,6 +16,8 @@
*/
package org.orekit.propagation.events.handlers;
import java.io.Serializable;
import org.orekit.errors.OrekitException;
import org.orekit.propagation.SpacecraftState;
import org.orekit.propagation.events.EventDetector;
......@@ -28,7 +30,10 @@ import org.orekit.propagation.events.EventDetector;
* @param <T> class type for the generic version
* @since 6.1
*/
public class StopOnEvent <T extends EventDetector> implements EventHandler<T> {
public class StopOnEvent <T extends EventDetector> implements EventHandler<T>, Serializable {
/** Serializable UID. */
private static final long serialVersionUID = 20160321L;
/**
* Specific implementation of the eventOccurred interface.
......
......@@ -16,6 +16,8 @@
*/
package org.orekit.propagation.events.handlers;
import java.io.Serializable;
import org.orekit.errors.OrekitException;
import org.orekit.propagation.SpacecraftState;
import org.orekit.propagation.events.EventDetector;
......@@ -31,7 +33,10 @@ import org.orekit.propagation.events.EventDetector;
* @param <T> class type for the generic version
* @since 6.1
*/
public class StopOnIncreasing<T extends EventDetector> implements EventHandler<T> {
public class StopOnIncreasing<T extends EventDetector> implements EventHandler<T>, Serializable {
/** Serializable UID. */
private static final long serialVersionUID = 20160321L;
/** Handle a detection event and choose what to do next.
* <p>The implementation behavior is to {@link
......
......@@ -16,7 +16,14 @@
*/
package org.orekit.propagation.events;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.NotSerializableException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
......@@ -318,6 +325,69 @@ public class EventEnablingPredicateFilterTest {
}
@Test
public void testNonSerializable() throws IOException {
try {
final EventEnablingPredicateFilter<DateDetector> filter =
new EventEnablingPredicateFilter<DateDetector>(new DateDetector(AbsoluteDate.J2000_EPOCH),
new DummyNonSerializablePredicate());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(filter);
Assert.fail("an exception should habe been thrown");
} catch (NotSerializableException nse) {
Assert.assertTrue(nse.getMessage().contains("DummyNonSerializablePredicate"));
}
}
private static class DummyNonSerializablePredicate implements EnablingPredicate<DateDetector> {
@Override
public boolean eventIsEnabled(final SpacecraftState state,
final DateDetector eventDetector,
final double g) {
return true;
}
}
@Test
public void testSerializable()
throws IOException, IllegalArgumentException, IllegalAccessException,
ClassNotFoundException, NoSuchFieldException, SecurityException {
final EventEnablingPredicateFilter<DateDetector> filter =
new EventEnablingPredicateFilter<DateDetector>(new DateDetector(AbsoluteDate.J2000_EPOCH),
new DummySerializablePredicate());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(filter);
Assert.assertTrue(bos.size() > 900);
Assert.assertTrue(bos.size() < 1000);
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bis);
@SuppressWarnings("unchecked")
EventEnablingPredicateFilter<DateDetector> deserialized =
(EventEnablingPredicateFilter<DateDetector>) ois.readObject();
Field enabler = EventEnablingPredicateFilter.class.getDeclaredField("enabler");
enabler.setAccessible(true);
Assert.assertEquals(DummySerializablePredicate.class, enabler.get(deserialized).getClass());
}
private static class DummySerializablePredicate implements EnablingPredicate<DateDetector>, Serializable {
private static final long serialVersionUID = 20160321L;
@Override
public boolean eventIsEnabled(final SpacecraftState state,
final DateDetector eventDetector,
final double g) {
return true;
}
}
@Before
public void setUp() throws OrekitException {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment