You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DeepCopy.java 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Copyright 2000-2013 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.sass.internal.util;
  17. import java.io.IOException;
  18. import java.io.ObjectInputStream;
  19. import java.io.ObjectOutputStream;
  20. import java.util.Collection;
  21. import java.util.LinkedList;
  22. import java.util.List;
  23. import java.util.logging.Level;
  24. import java.util.logging.Logger;
  25. /**
  26. * Utility for making deep copies (vs. clone()'s shallow copies) of objects.
  27. * Objects are first serialized and then deserialized. Error checking is fairly
  28. * minimal in this implementation. If an object is encountered that cannot be
  29. * serialized (or that references an object that cannot be serialized) an error
  30. * is printed to the logger and null is returned. Depending on your specific
  31. * application, it might make more sense to have copy(...) re-throw the
  32. * exception.
  33. */
  34. public class DeepCopy {
  35. /**
  36. * Returns a copy of the object, or null if the object cannot be serialized.
  37. */
  38. public static Object copy(Object orig) {
  39. Object obj = null;
  40. if (!(orig instanceof Clonable)) {
  41. try {
  42. // Write the object out to a byte array
  43. FastByteArrayOutputStream fbos = new FastByteArrayOutputStream();
  44. ObjectOutputStream out = new ObjectOutputStream(fbos);
  45. out.writeObject(orig);
  46. out.flush();
  47. out.close();
  48. // Retrieve an input stream from the byte array and read
  49. // a copy of the object back in.
  50. ObjectInputStream in = new ObjectInputStream(
  51. fbos.getInputStream());
  52. obj = in.readObject();
  53. in.close();
  54. } catch (IOException e) {
  55. log(e);
  56. } catch (ClassNotFoundException cnfe) {
  57. log(cnfe);
  58. }
  59. return obj;
  60. } else {
  61. try {
  62. obj = ((Clonable) orig).clone();
  63. } catch (ClassCastException e2) {
  64. // Can't clone, return obj as null
  65. } catch (CloneNotSupportedException e2) {
  66. // Can't clone, return obj as null
  67. }
  68. return obj;
  69. }
  70. }
  71. public static <T> Collection<T> copy(Collection<T> objects) {
  72. List<T> copies = new LinkedList<T>();
  73. for (T object : objects) {
  74. copies.add((T) copy(object));
  75. }
  76. return copies;
  77. }
  78. private static void log(Throwable e) {
  79. Logger.getLogger(DeepCopy.class.getName()).log(Level.SEVERE, null, e);
  80. }
  81. }