X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Ftemplate%2FCollectionAccessor.java;fp=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Ftemplate%2FCollectionAccessor.java;h=2b915928ce752ca769737935cd1d395d62d21a4d;hp=0000000000000000000000000000000000000000;hb=05f91b3345e9a6fcba39b805a1b34217f7372eee;hpb=d92dc06f1dc8929411d3f77804f8882aa860f78c diff --git a/src/main/java/net/pterodactylus/sone/template/CollectionAccessor.java b/src/main/java/net/pterodactylus/sone/template/CollectionAccessor.java new file mode 100644 index 0000000..2b91592 --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/template/CollectionAccessor.java @@ -0,0 +1,73 @@ +/* + * Sone - CollectionAccessor.java - Copyright © 2010 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sone.template; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +import net.pterodactylus.sone.data.Sone; +import net.pterodactylus.util.template.Accessor; +import net.pterodactylus.util.template.DataProvider; +import net.pterodactylus.util.template.ReflectionAccessor; + +/** + * {@link Accessor} for {@link Collection}s that adds a couple of specialized + * properties that only work for collections that contain the right types. + *
+ *
soneNames
+ *
Returns the nice names of all {@link Sone}s in the collection, sorted + * ascending by their nice names.
+ *
+ * + * @author David ‘Bombe’ Roden + */ +public class CollectionAccessor extends ReflectionAccessor { + + /** + * {@inheritDoc} + */ + @Override + public Object get(DataProvider dataProvider, Object object, String member) { + if (object == null) { + return null; + } + Collection collection = (Collection) object; + if (member.equals("soneNames")) { + List sones = new ArrayList(); + for (Object sone : collection) { + if (!(sone instanceof Sone)) { + continue; + } + sones.add((Sone) sone); + } + Collections.sort(sones, Sone.NICE_NAME_COMPARATOR); + StringBuilder soneNames = new StringBuilder(); + for (Sone sone : sones) { + if (soneNames.length() > 0) { + soneNames.append('\n'); + } + soneNames.append(SoneAccessor.getNiceName(sone)); + } + return soneNames.toString(); + } + return super.get(dataProvider, object, member); + } + +}