@Override public GlobalMetadata<S> copy() throws CopyNotSupportedException { if (CopyHelper.isCopyable(schema)) { return new GlobalMetadata((S)CopyHelper.copy(schema)); } throw new CopyNotSupportedException("Type is not copyable: " + schema.getClass().getName()); }
/** * Check if an object is copyable using the {@link #copy(Object)} method. * @param thing: the object that needs to be copied * @return: true if {@link CopyHelper} can copy this thing, false otherwise */ public static boolean isCopyable(Object thing) { if ( (thing instanceof Copyable) || (thing instanceof byte[]) || (isImmutableType(thing)) ) { return true; } return false; }
@Override protected StreamEntity<D> buildClone() { try { return new RecordEnvelope<>((D) CopyHelper.copy(_record), this, false); } catch (CopyNotSupportedException cnse) { throw new UnsupportedOperationException(cnse); } }
/** * 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 testUnsupportedTypes() throws CopyNotSupportedException { Object foobar = mock(ByteArrayInputStream.class); try { CopyHelper.copy(foobar); Assert.fail("Should throw exception"); } catch (CopyNotSupportedException cnse) { } catch (Exception e) { Assert.fail("Should not throw any exception other than CopyNotSupportedException. ", e); } }
if (inMultipleBranches(forkedSchemas) && !(CopyHelper.isCopyable(schema))) { throw new CopyNotSupportedException(schema + " is not copyable");
@Test public void testByteArray() throws CopyNotSupportedException { int length = RANDOM.nextInt(200); byte[] bytes = new byte[length]; RANDOM.nextBytes(bytes); Assert.assertTrue(CopyHelper.isCopyable(bytes)); byte[] copiedBytes = (byte[]) CopyHelper.copy(bytes); Assert.assertTrue(copiedBytes != bytes, "Copied bytes reference should be different for every copy after that"); Assert.assertEquals(copiedBytes, bytes, "Copied bytes value should be the same"); }
forkedStream.filter(new ForkFilter<>(idx)).map(RecordWithForkMap::getRecordCopyIfNecessary); forkStreams.add(inputStream.withRecordStream(thisStream, mustCopy ? (GlobalMetadata<S>) CopyHelper.copy(inputStream.getGlobalMetadata()) : inputStream.getGlobalMetadata())); } else {
/** * 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; }
/** * Check if an object is copyable using the {@link #copy(Object)} method. * @param thing: the object that needs to be copied * @return: true if {@link CopyHelper} can copy this thing, false otherwise */ public static boolean isCopyable(Object thing) { if ( (thing instanceof Copyable) || (thing instanceof byte[]) || (isImmutableType(thing)) ) { return true; } return false; }
@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); }
@Override protected StreamEntity<D> buildClone() { try { return new RecordEnvelope<>((D) CopyHelper.copy(_record), this, false); } catch (CopyNotSupportedException cnse) { throw new UnsupportedOperationException(cnse); } }
if (inMultipleBranches(forkedSchemas) && !(CopyHelper.isCopyable(schema))) { throw new CopyNotSupportedException(schema + " is not copyable");
@Test public void testImmutables() throws CopyNotSupportedException { Object nullObject = null; Integer integer = RANDOM.nextInt(200); byte[] bytes = new byte[integer]; RANDOM.nextBytes(bytes); String string = new String(bytes); Long longNum = RANDOM.nextLong(); Object[] immutables = new Object[]{nullObject, integer, string, longNum}; for (Object immutable : immutables) { Assert.assertTrue(CopyHelper.isCopyable(immutable)); for (int i=0; i < 2; ++i) { Object copiedObject = CopyHelper.copy(immutable); Assert.assertEquals(copiedObject, immutable); } } }
forkedStream.filter(new ForkFilter<>(idx)).map(RecordWithForkMap::getRecordCopyIfNecessary); forkStreams.add(inputStream.withRecordStream(thisStream, mustCopy ? (GlobalMetadata<S>) CopyHelper.copy(inputStream.getGlobalMetadata()) : inputStream.getGlobalMetadata())); } else {
if (needToCopy && !(CopyHelper.isCopyable(convertedRecord))) { throw new CopyNotSupportedException(convertedRecord.getClass().getName() + " is not copyable"); for (Optional<Fork> fork : this.forks.keySet()) { if (fork.isPresent() && forkedRecords.get(branch)) { Object recordForFork = needToCopy ? CopyHelper.copy(convertedRecord) : convertedRecord; copyInstance++; if (isStreamingTask()) {
@Override public GlobalMetadata<S> copy() throws CopyNotSupportedException { if (CopyHelper.isCopyable(schema)) { return new GlobalMetadata((S)CopyHelper.copy(schema)); } throw new CopyNotSupportedException("Type is not copyable: " + schema.getClass().getName()); }
if (needToCopy && !(CopyHelper.isCopyable(convertedRecord))) { throw new CopyNotSupportedException(convertedRecord.getClass().getName() + " is not copyable"); for (Optional<Fork> fork : this.forks.keySet()) { if (fork.isPresent() && forkedRecords.get(branch)) { Object recordForFork = needToCopy ? CopyHelper.copy(convertedRecord) : convertedRecord; copyInstance++; if (isStreamingTask()) {