Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WFLY-7802 Add externalizers for empty and singleton collections.
- Loading branch information
Showing
10 changed files
with
313 additions
and
211 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
...rc/main/java/org/wildfly/clustering/marshalling/spi/util/EmptyCollectionExternalizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,140 @@ | |||
/* | |||
* JBoss, Home of Professional Open Source. | |||
* Copyright 2016, Red Hat, Inc., and individual contributors | |||
* as indicated by the @author tags. See the copyright.txt file in the | |||
* distribution for a full listing of individual contributors. | |||
* | |||
* This is free software; you can redistribute it and/or modify it | |||
* under the terms of the GNU Lesser General Public License as | |||
* published by the Free Software Foundation; either version 2.1 of | |||
* the License, or (at your option) any later version. | |||
* | |||
* This software is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
* Lesser General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Lesser General Public | |||
* License along with this software; if not, write to the Free | |||
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | |||
* 02110-1301 USA, or see the FSF site: http://www.fsf.org. | |||
*/ | |||
|
|||
package org.wildfly.clustering.marshalling.spi.util; | |||
|
|||
import java.io.IOException; | |||
import java.io.ObjectInput; | |||
import java.io.ObjectOutput; | |||
import java.util.Collections; | |||
import java.util.Enumeration; | |||
import java.util.Iterator; | |||
import java.util.List; | |||
import java.util.ListIterator; | |||
import java.util.Map; | |||
import java.util.NavigableMap; | |||
import java.util.NavigableSet; | |||
import java.util.Set; | |||
import java.util.SortedMap; | |||
import java.util.SortedSet; | |||
import java.util.function.Supplier; | |||
|
|||
import org.kohsuke.MetaInfServices; | |||
import org.wildfly.clustering.marshalling.Externalizer; | |||
|
|||
/** | |||
* @author Paul Ferraro | |||
*/ | |||
public class EmptyCollectionExternalizer<T> implements Externalizer<T> { | |||
|
|||
private final Supplier<T> factory; | |||
|
|||
EmptyCollectionExternalizer(Supplier<T> factory) { | |||
this.factory = factory; | |||
} | |||
|
|||
@Override | |||
public void writeObject(ObjectOutput output, T object) throws IOException { | |||
// Nothing to write | |||
} | |||
|
|||
@Override | |||
public T readObject(ObjectInput input) throws IOException, ClassNotFoundException { | |||
return this.factory.get(); | |||
} | |||
|
|||
@SuppressWarnings("unchecked") | |||
@Override | |||
public Class<? extends T> getTargetClass() { | |||
return (Class<? extends T>) this.factory.get().getClass(); | |||
} | |||
|
|||
@MetaInfServices(Externalizer.class) | |||
public static class EmptyEnumerationExternalizer extends EmptyCollectionExternalizer<Enumeration<Object>> { | |||
public EmptyEnumerationExternalizer() { | |||
super(() -> Collections.emptyEnumeration()); | |||
} | |||
} | |||
|
|||
@MetaInfServices(Externalizer.class) | |||
public static class EmptyIteratorExternalizer extends EmptyCollectionExternalizer<Iterator<Object>> { | |||
public EmptyIteratorExternalizer() { | |||
super(() -> Collections.emptyIterator()); | |||
} | |||
} | |||
|
|||
@MetaInfServices(Externalizer.class) | |||
public static class EmptyListExternalizer extends EmptyCollectionExternalizer<List<Object>> { | |||
public EmptyListExternalizer() { | |||
super(() -> Collections.emptyList()); | |||
} | |||
} | |||
|
|||
@MetaInfServices(Externalizer.class) | |||
public static class EmptyListIteratorExternalizer extends EmptyCollectionExternalizer<ListIterator<Object>> { | |||
public EmptyListIteratorExternalizer() { | |||
super(() -> Collections.emptyListIterator()); | |||
} | |||
} | |||
|
|||
@MetaInfServices(Externalizer.class) | |||
public static class EmptyMapExternalizer extends EmptyCollectionExternalizer<Map<Object, Object>> { | |||
public EmptyMapExternalizer() { | |||
super(() -> Collections.emptyMap()); | |||
} | |||
} | |||
|
|||
@MetaInfServices(Externalizer.class) | |||
public static class EmptyNavigableMapExternalizer extends EmptyCollectionExternalizer<NavigableMap<Object, Object>> { | |||
public EmptyNavigableMapExternalizer() { | |||
super(() -> Collections.emptyNavigableMap()); | |||
} | |||
} | |||
|
|||
@MetaInfServices(Externalizer.class) | |||
public static class EmptyNavigableSetExternalizer extends EmptyCollectionExternalizer<NavigableSet<Object>> { | |||
public EmptyNavigableSetExternalizer() { | |||
super(() -> Collections.emptyNavigableSet()); | |||
} | |||
} | |||
|
|||
@MetaInfServices(Externalizer.class) | |||
public static class EmptySetExternalizer extends EmptyCollectionExternalizer<Set<Object>> { | |||
public EmptySetExternalizer() { | |||
super(() -> Collections.emptySet()); | |||
} | |||
} | |||
|
|||
@MetaInfServices(Externalizer.class) | |||
public static class EmptySortedMapExternalizer extends EmptyCollectionExternalizer<SortedMap<Object, Object>> { | |||
public EmptySortedMapExternalizer() { | |||
super(() -> Collections.emptySortedMap()); | |||
} | |||
} | |||
|
|||
@MetaInfServices(Externalizer.class) | |||
public static class EmptySortedSetExternalizer extends EmptyCollectionExternalizer<SortedSet<Object>> { | |||
public EmptySortedSetExternalizer() { | |||
super(() -> Collections.emptySortedSet()); | |||
} | |||
} | |||
} |
103 changes: 0 additions & 103 deletions
103
...alling/spi/src/main/java/org/wildfly/clustering/marshalling/spi/util/SetExternalizer.java
This file was deleted.
Oops, something went wrong.
77 changes: 77 additions & 0 deletions
77
...ain/java/org/wildfly/clustering/marshalling/spi/util/SingletonCollectionExternalizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,77 @@ | |||
/* | |||
* JBoss, Home of Professional Open Source. | |||
* Copyright 2016, Red Hat, Inc., and individual contributors | |||
* as indicated by the @author tags. See the copyright.txt file in the | |||
* distribution for a full listing of individual contributors. | |||
* | |||
* This is free software; you can redistribute it and/or modify it | |||
* under the terms of the GNU Lesser General Public License as | |||
* published by the Free Software Foundation; either version 2.1 of | |||
* the License, or (at your option) any later version. | |||
* | |||
* This software is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
* Lesser General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Lesser General Public | |||
* License along with this software; if not, write to the Free | |||
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | |||
* 02110-1301 USA, or see the FSF site: http://www.fsf.org. | |||
*/ | |||
|
|||
package org.wildfly.clustering.marshalling.spi.util; | |||
|
|||
import java.io.IOException; | |||
import java.io.ObjectInput; | |||
import java.io.ObjectOutput; | |||
import java.util.Collection; | |||
import java.util.Collections; | |||
import java.util.List; | |||
import java.util.Set; | |||
import java.util.function.Function; | |||
|
|||
import org.kohsuke.MetaInfServices; | |||
import org.wildfly.clustering.marshalling.Externalizer; | |||
|
|||
/** | |||
* @author Paul Ferraro | |||
*/ | |||
public class SingletonCollectionExternalizer<T extends Collection<Object>> implements Externalizer<T> { | |||
|
|||
private final Function<Object, T> factory; | |||
|
|||
SingletonCollectionExternalizer(Function<Object, T> factory) { | |||
this.factory = factory; | |||
} | |||
|
|||
@Override | |||
public void writeObject(ObjectOutput output, T collection) throws IOException { | |||
output.writeObject(collection.stream().findFirst().get()); | |||
} | |||
|
|||
@Override | |||
public T readObject(ObjectInput input) throws IOException, ClassNotFoundException { | |||
return this.factory.apply(input.readObject()); | |||
} | |||
|
|||
@SuppressWarnings("unchecked") | |||
@Override | |||
public Class<? extends T> getTargetClass() { | |||
return (Class<? extends T>) this.factory.apply(null).getClass(); | |||
} | |||
|
|||
@MetaInfServices(Externalizer.class) | |||
public static class SingletonListExternalizer extends SingletonCollectionExternalizer<List<Object>> { | |||
public SingletonListExternalizer() { | |||
super(Collections::singletonList); | |||
} | |||
} | |||
|
|||
@MetaInfServices(Externalizer.class) | |||
public static class SingletonSetExternalizer extends SingletonCollectionExternalizer<Set<Object>> { | |||
public SingletonSetExternalizer() { | |||
super(Collections::singleton); | |||
} | |||
} | |||
} |
Oops, something went wrong.