Author: echatellier Date: 2012-03-17 11:47:17 +0100 (Sat, 17 Mar 2012) New Revision: 2833 Url: http://chorem.org/repositories/revision/jtimer/2833 Log: Modify clone implementation (just soft clone now) Modified: trunk/src/main/java/org/chorem/jtimer/entities/TimerTask.java trunk/src/test/java/org/chorem/jtimer/entities/TimerTaskTest.java Modified: trunk/src/main/java/org/chorem/jtimer/entities/TimerTask.java =================================================================== --- trunk/src/main/java/org/chorem/jtimer/entities/TimerTask.java 2012-03-17 09:58:17 UTC (rev 2832) +++ trunk/src/main/java/org/chorem/jtimer/entities/TimerTask.java 2012-03-17 10:47:17 UTC (rev 2833) @@ -33,21 +33,16 @@ import java.util.TreeMap; import java.util.UUID; -import org.apache.commons.lang3.SerializationUtils; import org.chorem.jtimer.utils.DailySortedMap; /** * Represents a task. * - * This task is cloneable using SerializationUtils (commons-lang). - * * @author chatellier * @version $Revision$ * * Last update : $Date$ * By : $Author$ - * - * @see SerializationUtils */ public class TimerTask implements Cloneable, Comparable<TimerTask>, Serializable { @@ -369,27 +364,14 @@ try { task = (TimerTask) super.clone(); - // clone collections using - // commons-lang SerializationUtils - - // copy name - task.name = name; - // copy date task.creationDate = creationDate == null ? null : (Date) creationDate.clone(); - // copy durations - task.allDaysTimes = (DailySortedMap<Long>) SerializationUtils - .clone((DailySortedMap<Long>) allDaysTimes); - - // copy notes - task.allDaysAnnotations = (TreeMap<Date, String>) SerializationUtils - .clone((TreeMap<Date, String>) allDaysAnnotations); - - // copy subtasks - task.subTasks = (ArrayList<TimerTask>) SerializationUtils - .clone((ArrayList<TimerTask>) subTasks); + // make new list instance + task.allDaysTimes = new DailySortedMap<Long>(allDaysTimes); + task.allDaysAnnotations = new TreeMap<Date, String>(allDaysAnnotations); + task.subTasks = new ArrayList<TimerTask>(subTasks); } catch (CloneNotSupportedException e) { throw new RuntimeException("Can't clone", e); } Modified: trunk/src/test/java/org/chorem/jtimer/entities/TimerTaskTest.java =================================================================== --- trunk/src/test/java/org/chorem/jtimer/entities/TimerTaskTest.java 2012-03-17 09:58:17 UTC (rev 2832) +++ trunk/src/test/java/org/chorem/jtimer/entities/TimerTaskTest.java 2012-03-17 10:47:17 UTC (rev 2833) @@ -5,7 +5,7 @@ * $Id$ * $HeadURL$ * %% - * Copyright (C) 2008 - 2011 CodeLutin, Chatellier Eric + * Copyright (C) 2008 - 2012 CodeLutin, Chatellier Eric * %% * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as @@ -25,7 +25,6 @@ package org.chorem.jtimer.entities; -import java.util.Calendar; import java.util.Date; import org.chorem.jtimer.AbstractJTimerTest; @@ -70,7 +69,7 @@ Assert.assertTrue(clonedTask.allDaysTimes instanceof DailySortedMap<?>, "Clone put invalid type for map"); Assert.assertFalse(clonedTask.allDaysAnnotations instanceof DailySortedMap<?>, "Clone put invalid type for map"); } - + /** * Test task subtasks. */ @@ -91,7 +90,6 @@ Assert.assertEquals(1, clonedTask.getSubTasks().size()); // subtask modification - subtask.setName("bobo"); Assert.assertEquals("subtest", clonedTask.getSubTasks().get(0).getName()); // clone removal, not modify first @@ -99,4 +97,26 @@ Assert.assertEquals(0, clonedTask.getSubTasks().size()); Assert.assertEquals(1, task.getSubTasks().size()); } + + /** + * Test that clone is a soft clone (excepted for lists). + */ + @Test + public void testCloneLists() { + TimerTask task = new TimerTask(); + task.setName("test"); + + TimerTask clonedTask = task.clone(); + + task.setClosed(true); + task.addTask(new TimerTask()); + task.setName("new name"); + + Assert.assertEquals(task.getName(), "new name"); + Assert.assertEquals(clonedTask.getName(), "test"); + Assert.assertTrue(task.isClosed()); + Assert.assertFalse(clonedTask.isClosed()); + Assert.assertEquals(task.getSubTasks().size(), 1); + Assert.assertEquals(clonedTask.getSubTasks().size(), 0); + } }