Example usage for org.apache.commons.lang3 SerializationUtils clone

List of usage examples for org.apache.commons.lang3 SerializationUtils clone

Introduction

In this page you can find the example usage for org.apache.commons.lang3 SerializationUtils clone.

Prototype

public static <T extends Serializable> T clone(final T object) 

Source Link

Document

Deep clone an Object using serialization.

This is many times slower than writing clone methods by hand on all objects in your object graph.

Usage

From source file:org.grouplens.lenskit.data.dao.packed.LimitedBinaryRatingDAOTest.java

@Test
public void testSerializedDAO() throws IOException {
    BinaryRatingDAO brDao = dao.createWindowedView(1650L);
    BinaryRatingDAO clone = SerializationUtils.clone(brDao);
    verifyDAO(clone);/*  w w w  .j  a v  a  2  s  .  c  o  m*/
}

From source file:org.grouplens.lenskit.scored.RandomScoredIdListTest.java

@Test
public void testSerialize() {
    PackedScoredIdList list = builder.finish();
    PackedScoredIdList l2 = SerializationUtils.clone(list);
    assertThat(l2, not(sameInstance(list)));
    assertThat(l2, equalTo(list));//from w w w. j a  va 2  s. c o m
}

From source file:org.grouplens.lenskit.symbols.SymbolsTest.java

@Test
public void testSerialize() {
    Symbol sbar = Symbol.of("bar");
    Symbol cloned = SerializationUtils.clone(sbar);
    assertThat(cloned, sameInstance(sbar));
}

From source file:org.grouplens.lenskit.symbols.TestTypedSymbols.java

@Test
public void testSerialize() {
    TypedSymbol<InputStream> sbar = TypedSymbol.of(InputStream.class, "ratings");
    TypedSymbol<InputStream> cloned = SerializationUtils.clone(sbar);
    assertThat(cloned, sameInstance(sbar));
}

From source file:org.hbird.exchange.core.EntityInstance.java

@SuppressWarnings("unchecked")
public <T extends EntityInstance> T cloneEntity() {
    EntityInstance newInstance = SerializationUtils.clone(this);
    newInstance.setTimestamp(System.currentTimeMillis());
    return (T) newInstance;
}

From source file:org.lenskit.data.entities.EntityTypeTest.java

@Test
public void testSerialize() {
    EntityType wombat = EntityType.forName("wombat");

    EntityType cloned = SerializationUtils.clone(wombat);
    assertThat(cloned, sameInstance(wombat));
}

From source file:org.lenskit.data.entities.TypedNameTest.java

@Test
public void testSerialize() {
    TypedName<String> attribute = TypedName.create("foo", String.class);
    assertThat(SerializationUtils.clone(attribute), sameInstance(attribute));
}

From source file:org.mashupmedia.service.MapperManagerImpl.java

@Override
public void writeSongToXml(long libraryId, Song song) throws Exception {
    if (song == null) {
        return;/*  ww  w  .  ja v  a 2 s . co  m*/
    }

    Song clonedSong = SerializationUtils.clone(song);
    clonedSong.setId(0);
    clonedSong.setPath(String.valueOf(song.getId()));

    String fileName = clonedSong.getFileName();
    clonedSong.setFileName(StringHelper.escapeXml(fileName));

    String songTitle = clonedSong.getTitle();
    clonedSong.setTitle(StringHelper.escapeXml(songTitle));

    String songSearchText = clonedSong.getSearchText();
    clonedSong.setSearchText(StringHelper.escapeXml(songSearchText));

    String summary = clonedSong.getSummary();
    clonedSong.setSummary(StringHelper.escapeXml(summary));

    String displayTitle = clonedSong.getDisplayTitle();
    clonedSong.setDisplayTitle(StringHelper.escapeXml(displayTitle));

    Artist clonedArtist = SerializationUtils.clone(song.getArtist());
    String artistName = clonedArtist.getName();
    clonedArtist.setName(StringHelper.escapeXml(artistName));
    String artistIndexText = clonedArtist.getIndexText();
    clonedArtist.setIndexText(StringHelper.escapeXml(artistIndexText));
    clonedSong.setArtist(clonedArtist);

    Album clonedAlbum = SerializationUtils.clone(song.getAlbum());
    String albumName = clonedAlbum.getName();
    clonedAlbum.setName(StringHelper.escapeXml(albumName));
    String albumFolderName = clonedAlbum.getFolderName();
    clonedAlbum.setFolderName(StringHelper.escapeXml(albumFolderName));
    clonedSong.setAlbum(clonedAlbum);

    File file = FileHelper.getLibraryXmlFile(libraryId);

    FileWriter writer = new FileWriter(file, true);
    getMarshaller().marshal(clonedSong, new StreamResult(writer));
    writer.close();
}

From source file:org.mayocat.configuration.internal.ConfigurationJsonMerger.java

public Map<String, Serializable> merge() {
    return SerializationUtils.clone(merge(platform, tenant));
}

From source file:org.mayocat.localization.internal.DefaultEntityLocalizationService.java

@Override
public <T extends Localized> T localize(T entity, Locale locale) {
    if (locale == null || entity.getLocalizedVersions() == null
            || !entity.getLocalizedVersions().containsKey(locale)) {
        return entity;
    }/*www .  j ava  2 s . co m*/

    T copiedEntity = SerializationUtils.clone(entity);

    if (copiedEntity == null) {
        return entity;
    }

    // Special I/O case for loaded attachment : set back the input stream manually
    if (copiedEntity instanceof LoadedAttachment) {
        ((LoadedAttachment) copiedEntity)
                .setData(new AttachmentData(((LoadedAttachment) entity).getData().getStream()));
    }

    // Handle entity fields :
    // - loops over methods, checking for setters, then for each one of them
    // - infer a field name from found setter
    // - check if that field has a "LocalizedField" annotation, if not ignore
    // - if it does, try to find this field in the locale's map of translations
    // - if found and not null or empty string use the setter to set this as the localized field value

    for (Method method : copiedEntity.getClass().getDeclaredMethods()) {
        if (method.getName().startsWith("set") && Character.isUpperCase(method.getName().charAt(3))) {
            // Found a setter.
            if (method.getName().length() <= 4) {
                continue;
            }
            String fieldName = method.getName().substring(3, 4).toLowerCase() + method.getName().substring(4);

            Field field = null;
            try {
                field = copiedEntity.getClass().getDeclaredField(fieldName);
            } catch (NoSuchFieldException e) {
                this.logger.debug("Cannot find field for setter {}", method.getName());
            }

            // Check if either field or method has a "LocalizedField" annotation
            if (field != null && (field.isAnnotationPresent(LocalizedField.class)
                    || method.isAnnotationPresent(LocalizedField.class))) {
                Object value = null;
                if (copiedEntity.getLocalizedVersions().get(locale).containsKey(fieldName)) {

                    value = copiedEntity.getLocalizedVersions().get(locale).get(fieldName);

                    if (String.class.isAssignableFrom(value.getClass())
                            && Strings.isNullOrEmpty((String) value)) {
                        // Ignore empty strings, consider them as nulls
                        continue;
                    }

                    boolean setterAccessible = method.isAccessible();
                    method.setAccessible(true);
                    try {
                        method.invoke(copiedEntity, value);
                    } catch (IllegalAccessException | InvocationTargetException e) {
                        logger.error("Cannot set property {}", field.getName());
                    }

                    method.setAccessible(setterAccessible);
                }
            }
        }
    }

    // Handle entity addons :
    // - check if entity has addons and those addons are loaded, and the localized version contains something
    // for addons
    // - if yes, then loop over all the entity addons, and for each :
    // - try to find the equivalent addon in the map of translation
    // - if found, and its value is not null or empty string, replace the addon value by the localized one

    if (hasLoadedAddons(copiedEntity)
            && copiedEntity.getLocalizedVersions().get(locale).containsKey("addons")) {
        Map<String, AddonGroup> entityAddons = ((HasAddons) copiedEntity).getAddons().get();
        final Map<String, Object> localizedAddons = (Map<String, Object>) copiedEntity.getLocalizedVersions()
                .get(locale).get("addons");

        for (AddonGroup addon : entityAddons.values()) {
            if (localizedAddons.containsKey(addon.getGroup())) {
                Map<String, Object> localizedGroup = (Map<String, Object>) localizedAddons
                        .get(addon.getGroup());

                if (Map.class.isAssignableFrom(addon.getValue().getClass())) {

                    // Non-sequence addons

                    Map<String, Object> value = (Map<String, Object>) addon.getValue();
                    Map<String, Object> localizedGroupValue = (Map<String, Object>) localizedGroup.get("value");

                    for (String field : value.keySet()) {
                        Object localizedValue = localizedGroupValue.get(field);

                        if (localizedValue == null || (String.class.isAssignableFrom(localizedValue.getClass())
                                && Strings.isNullOrEmpty((String) localizedValue))) {
                            // Ignore empty strings, consider them as nulls
                            continue;
                        }

                        ((Map<String, Object>) addon.getValue()).put(field, localizedValue);
                    }
                } else if (List.class.isAssignableFrom(addon.getValue().getClass())) {

                    // Sequence addons

                    List<Map<String, Object>> values = (List<Map<String, Object>>) addon.getValue();
                    try {
                        List<Map<String, Object>> localizedGroupValue = (List<Map<String, Object>>) localizedGroup
                                .get("value");
                        Integer i = 0;
                        for (Map<String, Object> value : values) {
                            Map<String, Object> localizedGroupValueItem = localizedGroupValue.get(i);

                            for (String field : value.keySet()) {
                                Object localizedValue = localizedGroupValueItem.get(field);

                                if (localizedValue == null
                                        || (String.class.isAssignableFrom(localizedValue.getClass())
                                                && Strings.isNullOrEmpty((String) localizedValue))) {
                                    // Ignore empty strings, consider them as nulls
                                    continue;
                                }

                                ((List<Map<String, Object>>) addon.getValue()).get(i).put(field,
                                        localizedValue);
                            }
                            i++;
                        }
                    } catch (ClassCastException e) {
                        // Ignore...
                    }
                }
            }
        }
    }

    return copiedEntity;
}