/** * Copy this object if needed. * @param thing : this object that needs to be copied * @return: a possibly copied instance * @throws CopyNotSupportedException if thing needs to be copied but cannot be */ public static Object copy(Object thing) throws CopyNotSupportedException { if (!isCopyable(thing)) { throw new CopyNotSupportedException(thing.getClass().getName() + " cannot be copied. See Copyable"); } if (thing instanceof Copyable) { return ((Copyable) thing).copy(); } // Support for a few primitive types out of the box if (thing instanceof byte[]) { byte[] copy = new byte[((byte[]) thing).length]; System.arraycopy(thing, 0, copy, 0, ((byte[]) thing).length); return copy; } // Assume that everything other type is immutable, not checking this again return thing; }
@Test public void testCopyable() throws CopyNotSupportedException { Copyable c = mock(Copyable.class); Assert.assertTrue(CopyHelper.isCopyable(c)); Object copy = new Object(); when(c.copy()).thenReturn(copy); Assert.assertEquals(CopyHelper.copy(c), copy); Assert.assertEquals(CopyHelper.copy(c), copy); }
if (forkedSchemas.get(i)) { AsynchronousFork fork = closer.register( new AsynchronousFork(this.taskContext, schema instanceof Copyable ? ((Copyable) schema).copy() : schema, branches, i, this.taskMode)); configureStreamingFork(fork, watermarkingStrategy); new SynchronousFork(this.taskContext, schema instanceof Copyable ? ((Copyable) schema).copy() : schema, branches, 0, this.taskMode)); configureStreamingFork(fork, watermarkingStrategy);
/** * Copy this object if needed. * @param thing : this object that needs to be copied * @return: a possibly copied instance * @throws CopyNotSupportedException if thing needs to be copied but cannot be */ public static Object copy(Object thing) throws CopyNotSupportedException { if (!isCopyable(thing)) { throw new CopyNotSupportedException(thing.getClass().getName() + " cannot be copied. See Copyable"); } if (thing instanceof Copyable) { return ((Copyable) thing).copy(); } // Support for a few primitive types out of the box if (thing instanceof byte[]) { byte[] copy = new byte[((byte[]) thing).length]; System.arraycopy(thing, 0, copy, 0, ((byte[]) thing).length); return copy; } // Assume that everything other type is immutable, not checking this again return thing; }
if (forkedSchemas.get(i)) { AsynchronousFork fork = closer.register( new AsynchronousFork(this.taskContext, schema instanceof Copyable ? ((Copyable) schema).copy() : schema, branches, i, this.taskMode)); configureStreamingFork(fork, watermarkingStrategy); new SynchronousFork(this.taskContext, schema instanceof Copyable ? ((Copyable) schema).copy() : schema, branches, 0, this.taskMode)); configureStreamingFork(fork, watermarkingStrategy);