List of usage examples for com.google.common.collect Multimap clear
void clear();
From source file:io.mapzone.arena.analytics.graph.OrganisationPersonGraphFunction.java
@Override public void generate(MdToolkit tk, IProgressMonitor monitor, Graph graph) throws Exception { if (!tk.isClosed()) { tk.createSnackbar(Appearance.FadeIn, "Analysis started - stay tuned..."); }//from w w w . j a v a2 s . c om final Map<String, Node> organisations = Maps.newHashMap(); final Map<String, Node> persons = Maps.newHashMap(); final Multimap<Node, Node> organisation2Persons = ArrayListMultimap.create(); final Multimap<Node, Node> person2Organisations = ArrayListMultimap.create(); // iterate on features // create Node for each organisation // increase weight for each entry per organisation FeatureIterator iterator = featureSource.getFeatures().features(); int i = 0; while (iterator.hasNext() && i < 5000) { i++; SimpleFeature feature = (SimpleFeature) iterator.next(); String organisationKey = (String) feature.getAttribute("Organisation"); Node organisationFeature = organisations.get(organisationKey); if (organisationFeature == null) { organisationFeature = new Node(Node.Type.virtual, "o:" + feature.getID(), featureSource, feature, organisationKey, 1); organisations.put(organisationKey, organisationFeature); graph.addOrUpdateNode(organisationFeature); } else { // add weight int size = organisation2Persons.get(organisationFeature).size() + 1; if (size <= 15) { organisationFeature.increaseWeight(); } graph.addOrUpdateNode(organisationFeature); } String personKey = (String) feature.getAttribute("Name") + " " + (String) feature.getAttribute("Vorname"); Node personFeature = persons.get(personKey); if (personFeature == null) { personFeature = new Node(Node.Type.real, "p:" + feature.getID(), featureSource, feature, personKey, 1); persons.put(personKey, personFeature); graph.addOrUpdateNode(personFeature); } else { int size = person2Organisations.get(personFeature).size() + 1; if (size <= 15) { personFeature.increaseWeight(); } graph.addOrUpdateNode(personFeature); } // add also the person to the organisation organisation2Persons.put(organisationFeature, personFeature); person2Organisations.put(personFeature, organisationFeature); graph.addOrUpdateEdge(organisationFeature, personFeature); if (i % 100 == 0) { log.info("added " + i); } } if (!tk.isClosed()) { tk.createSnackbar(Appearance.FadeIn, organisations.size() + " organisations, " + persons.size() + " persons and " + organisation2Persons.size() + " relations analysed"); } organisations.clear(); persons.clear(); organisation2Persons.clear(); person2Organisations.clear(); graph.layout(); }
From source file:org.openflexo.dg.action.ReinjectDocx.java
public Multimap<EditionPatternInstance, IParsedFlexoEPI> removeConflictingParsedDocX( Multimap<EditionPatternInstance, IParsedFlexoEPI> epis) { Multimap<EditionPatternInstance, IParsedFlexoEPI> episToReinject = ArrayListMultimap.create(); Multimap<String, IParsedFlexoEPI> paths = ArrayListMultimap.create(); for (Entry<EditionPatternInstance, Collection<IParsedFlexoEPI>> e : epis.asMap().entrySet()) { if (e.getValue().size() > 1) { // There are multiple parsed DocX EPI for this EditionPatternInstance // Let's see if it is for the same binding path for (IParsedFlexoEPI epi : e.getValue()) { paths.put(epi.getBindingPath(), epi); }/*from w w w . j av a2 s . c o m*/ for (Entry<String, Collection<IParsedFlexoEPI>> e1 : paths.asMap().entrySet()) { boolean conflict = false; if (e1.getValue().size() > 1) { // There are multiple parsed DocX EPI for the same EPI and the same binding path Object currentValue = e.getKey().evaluate(e1.getKey()); List<IParsedFlexoEPI> modified = new ArrayList<IParsedFlexoEPI>(); for (IParsedFlexoEPI epi : e1.getValue()) { if (!epi.getValue().equals(currentValue)) { modified.add(epi); } } if (modified.size() > 1) { // There is more than one parsed DocX EPI that has a different value than the current one // Let's see if they are not all the same. String value = modified.get(0).getValue(); for (int i = 1; i < modified.size(); i++) { if (!value.equals(modified.get(i).getValue())) { conflict = true; errorReport.append("Conflicting values: ").append(value + " ") .append(modified.get(i).getValue()).append("\n"); break; } } } } if (!conflict) { episToReinject.putAll(e.getKey(), e1.getValue()); } } } else { // There is a single parsed DocX EPI for this EditionPatternInstance episToReinject.putAll(e.getKey(), e.getValue()); } paths.clear(); } return episToReinject; }
From source file:com.github.haixing_hu.io.InputUtils.java
/** * Reads a multimap of objects from the input stream. * * @param K/*from w ww. j a v a2 s . c om*/ * the type of the keys of the multimap to be read. The binary * serializer of the class {@code K} must have already been * registered. * @param V * the type of the values of the multimap to be read. The binary * serializer of the class {@code V} must have already been * registered. * @param keyClass * the class of the keys of the multimap to be read. * @param valueClass * the class of the values of the multimap to be read. * @param in * a binary input stream. * @param allowNullMap * if it is true, the multimap to be read could be null; otherwise, * if the multimap read from the input is null, an * {@code InvalidFormatException} will be thrown. * @param allowNullKey * if it is true, the key in the multimap to be read could be null; * otherwise, if any key of the multimap read from the input is null, * an {@code InvalidFormatException} will be thrown. * @param allowNullValue * if it is true, the value in the multimap to be read could be null; * otherwise, if any value of the multimap read from the input is * null, an {@code InvalidFormatException} will be thrown. * @param result * a multimap used to store the result. It could be null. * @return a multimap read from the input. The returned multimap may be null * if {@code allowNullMap} is true; the key in the returned * multimap may be null if {@code allowNullKey} is true; and the * value in the returned multimap may be null if * {@code allowNullValue} is true. * @throws EOFException * if the input reaches the end before reading the whole multimap. * @throws InvalidFormatException * if the multimap read from the input is null, while the argument * {@code allowNullMap} is false; or any key in the multimap * read from the input is null, while the argument * {@code allowNullKey} is false; or any value in the multimap * read from the input is null, while the argument * {@code allowNullValue} is false. * @throws IOException * if any I/O other error occurs. */ public static <K, V> Multimap<K, V> readMultimap(final Class<K> keyClass, final Class<V> valueClass, final InputStream in, final boolean allowNullMap, final boolean allowNullKey, final boolean allowNullValue, @Nullable Multimap<K, V> result) throws IOException { final BinarySerializer keySerializer = BinarySerialization.getSerializer(keyClass); if (keySerializer == null) { throw new NoBinarySerializerRegisteredException(keyClass); } final BinarySerializer valueSerializer = BinarySerialization.getSerializer(valueClass); if (valueSerializer == null) { throw new NoBinarySerializerRegisteredException(valueClass); } if (readNullMark(in)) { if (allowNullMap) { return null; } else { throw new InvalidFormatException(UNEXPECTED_NULL_VALUE); } } final int n = readVarInt(in); if (n == 0) { if (result != null) { result.clear(); return result; } else { return LinkedHashMultimap.create(); } } else { if (result == null) { result = LinkedHashMultimap.create(); } else { result.clear(); } try { for (int i = 0; i < n; ++i) { @SuppressWarnings("unchecked") final K key = (K) keySerializer.deserialize(in, allowNullKey); @SuppressWarnings("unchecked") final V value = (V) valueSerializer.deserialize(in, allowNullValue); result.put(key, value); } } catch (final ClassCastException e) { throw new SerializationException(e); } return result; } }
From source file:org.tomahawk.libtomahawk.infosystem.hatchet.HatchetInfoPlugin.java
/** * Core method of this InfoPlugin. Gets and parses the ordered results. * * @param infoRequestData InfoRequestData object containing the input parameters. * @return true if the type of the given InfoRequestData was valid and could be processed. false * otherwise/*from w w w. ja v a 2s .co m*/ */ private boolean getAndParseInfo(InfoRequestData infoRequestData) throws NoSuchAlgorithmException, KeyManagementException, IOException { long start = System.currentTimeMillis(); Multimap<String, String> params = LinkedListMultimap.create(); Map<String, Map> resultMapList = new HashMap<String, Map>(); String rawJsonString; if (infoRequestData.getType() == InfoRequestData.INFOREQUESTDATA_TYPE_USERS) { rawJsonString = TomahawkUtils.httpGet(buildQuery(InfoRequestData.INFOREQUESTDATA_TYPE_USERS, infoRequestData.getParams())).mResponseText; infoRequestData.setInfoResult(mObjectMapper.readValue(rawJsonString, HatchetUsers.class)); return true; } else if (infoRequestData.getType() == InfoRequestData.INFOREQUESTDATA_TYPE_USERS_SELF) { if (TextUtils.isEmpty(mUserId)) { return false; } params.put(HATCHET_PARAM_IDARRAY, mUserId); rawJsonString = TomahawkUtils .httpGet(buildQuery(InfoRequestData.INFOREQUESTDATA_TYPE_USERS, params)).mResponseText; infoRequestData.setInfoResult(mObjectMapper.readValue(rawJsonString, HatchetUsers.class)); return true; } else if (infoRequestData.getType() == InfoRequestData.INFOREQUESTDATA_TYPE_USERS_PLAYLISTS) { if (TextUtils.isEmpty(mUserId)) { return false; } params.put(HATCHET_PARAM_ID, mUserId); rawJsonString = TomahawkUtils.httpGet( buildQuery(InfoRequestData.INFOREQUESTDATA_TYPE_USERS_PLAYLISTS, params)).mResponseText; infoRequestData.setInfoResult(mObjectMapper.readValue(rawJsonString, HatchetPlaylists.class)); return true; } else if (infoRequestData.getType() == InfoRequestData.INFOREQUESTDATA_TYPE_PLAYLISTS_ENTRIES) { rawJsonString = TomahawkUtils.httpGet(buildQuery(InfoRequestData.INFOREQUESTDATA_TYPE_PLAYLISTS_ENTRIES, infoRequestData.getParams())).mResponseText; infoRequestData.setInfoResult(mObjectMapper.readValue(rawJsonString, HatchetPlaylistEntries.class)); return true; } else if (infoRequestData.getType() == InfoRequestData.INFOREQUESTDATA_TYPE_USERS_LOVEDITEMS) { if (TextUtils.isEmpty(mUserId)) { return false; } Map<HatchetPlaylistInfo, HatchetPlaylistEntries> playlistEntriesMap = new HashMap<HatchetPlaylistInfo, HatchetPlaylistEntries>(); params.put(HATCHET_PARAM_ID, mUserId); rawJsonString = TomahawkUtils.httpGet( buildQuery(InfoRequestData.INFOREQUESTDATA_TYPE_USERS_LOVEDITEMS, params)).mResponseText; HatchetPlaylistEntries playlistEntries = mObjectMapper.readValue(rawJsonString, HatchetPlaylistEntries.class); playlistEntriesMap.put(playlistEntries.playlist, playlistEntries); resultMapList.put(HATCHET_PLAYLISTS_ENTRIES, playlistEntriesMap); infoRequestData.setInfoResultMap(resultMapList); return true; } else if (infoRequestData.getType() == InfoRequestData.INFOREQUESTDATA_TYPE_USERS_SOCIALACTIONS) { rawJsonString = TomahawkUtils .httpGet(buildQuery(InfoRequestData.INFOREQUESTDATA_TYPE_USERS_SOCIALACTIONS, infoRequestData.getParams())).mResponseText; infoRequestData .setInfoResult(mObjectMapper.readValue(rawJsonString, HatchetSocialActionResponse.class)); return true; } else if (infoRequestData.getType() == InfoRequestData.INFOREQUESTDATA_TYPE_USERS_FRIENDSFEED) { rawJsonString = TomahawkUtils.httpGet(buildQuery(InfoRequestData.INFOREQUESTDATA_TYPE_USERS_FRIENDSFEED, infoRequestData.getParams())).mResponseText; infoRequestData .setInfoResult(mObjectMapper.readValue(rawJsonString, HatchetSocialActionResponse.class)); return true; } else if (infoRequestData.getType() == InfoRequestData.INFOREQUESTDATA_TYPE_USERS_PLAYBACKLOG) { rawJsonString = TomahawkUtils.httpGet(buildQuery(InfoRequestData.INFOREQUESTDATA_TYPE_USERS_PLAYBACKLOG, infoRequestData.getParams())).mResponseText; infoRequestData .setInfoResult(mObjectMapper.readValue(rawJsonString, HatchetPlaybackLogsResponse.class)); return true; } else if (infoRequestData.getType() == InfoRequestData.INFOREQUESTDATA_TYPE_ARTISTS) { rawJsonString = TomahawkUtils.httpGet(buildQuery(InfoRequestData.INFOREQUESTDATA_TYPE_ARTISTS, infoRequestData.getParams())).mResponseText; infoRequestData.setInfoResult(mObjectMapper.readValue(rawJsonString, HatchetArtists.class)); return true; } else if (infoRequestData.getType() == InfoRequestData.INFOREQUESTDATA_TYPE_ARTISTS_ALBUMS) { rawJsonString = TomahawkUtils.httpGet(buildQuery(InfoRequestData.INFOREQUESTDATA_TYPE_ARTISTS, infoRequestData.getParams())).mResponseText; HatchetArtists artists = mObjectMapper.readValue(rawJsonString, HatchetArtists.class); if (artists.artists != null && artists.artists.size() > 0) { Map<HatchetAlbumInfo, HatchetTracks> tracksMap = new HashMap<HatchetAlbumInfo, HatchetTracks>(); Map<HatchetAlbumInfo, HatchetImage> imageMap = new HashMap<HatchetAlbumInfo, HatchetImage>(); params.put(HATCHET_PARAM_ID, artists.artists.get(0).id); rawJsonString = TomahawkUtils.httpGet( buildQuery(InfoRequestData.INFOREQUESTDATA_TYPE_ARTISTS_ALBUMS, params)).mResponseText; HatchetCharts charts = mObjectMapper.readValue(rawJsonString, HatchetCharts.class); Map<String, HatchetImage> chartImageMap = new HashMap<String, HatchetImage>(); if (charts.images != null) { for (HatchetImage image : charts.images) { chartImageMap.put(image.id, image); } } if (charts.albums != null) { for (HatchetAlbumInfo albumInfo : charts.albums) { if (albumInfo.images != null && albumInfo.images.size() > 0) { imageMap.put(albumInfo, chartImageMap.get(albumInfo.images.get(0))); } if (albumInfo.tracks != null && albumInfo.tracks.size() > 0) { params.clear(); for (String trackId : albumInfo.tracks) { params.put(HATCHET_PARAM_IDARRAY, trackId); } rawJsonString = TomahawkUtils.httpGet( buildQuery(InfoRequestData.INFOREQUESTDATA_TYPE_TRACKS, params)).mResponseText; HatchetTracks tracks = mObjectMapper.readValue(rawJsonString, HatchetTracks.class); tracksMap.put(albumInfo, tracks); } } } resultMapList.put(HATCHET_TRACKS, tracksMap); resultMapList.put(HATCHET_IMAGES, imageMap); } infoRequestData.setInfoResultMap(resultMapList); return true; } else if (infoRequestData.getType() == InfoRequestData.INFOREQUESTDATA_TYPE_ARTISTS_TOPHITS) { rawJsonString = TomahawkUtils.httpGet(buildQuery(InfoRequestData.INFOREQUESTDATA_TYPE_ARTISTS, infoRequestData.getParams())).mResponseText; HatchetArtists artists = mObjectMapper.readValue(rawJsonString, HatchetArtists.class); if (artists.artists != null && artists.artists.size() > 0) { Map<HatchetChartItem, HatchetTrackInfo> tracksMap = new LinkedHashMap<HatchetChartItem, HatchetTrackInfo>(); params.put(HATCHET_PARAM_ID, artists.artists.get(0).id); rawJsonString = TomahawkUtils.httpGet( buildQuery(InfoRequestData.INFOREQUESTDATA_TYPE_ARTISTS_TOPHITS, params)).mResponseText; HatchetCharts charts = mObjectMapper.readValue(rawJsonString, HatchetCharts.class); Map<String, HatchetTrackInfo> trackInfoMap = new HashMap<String, HatchetTrackInfo>(); if (charts.tracks != null) { for (HatchetTrackInfo trackInfo : charts.tracks) { trackInfoMap.put(trackInfo.id, trackInfo); } } if (charts.chartItems != null) { for (HatchetChartItem chartItem : charts.chartItems) { tracksMap.put(chartItem, trackInfoMap.get(chartItem.track)); } } resultMapList.put(HATCHET_TRACKS, tracksMap); } infoRequestData.setInfoResultMap(resultMapList); return true; } else if (infoRequestData.getType() == InfoRequestData.INFOREQUESTDATA_TYPE_ALBUMS) { rawJsonString = TomahawkUtils.httpGet(buildQuery(InfoRequestData.INFOREQUESTDATA_TYPE_ALBUMS, infoRequestData.getParams())).mResponseText; HatchetAlbums albums = mObjectMapper.readValue(rawJsonString, HatchetAlbums.class); if (albums.albums != null && albums.albums.size() > 0) { HatchetAlbumInfo albumInfo = albums.albums.get(0); Map<String, HatchetImage> imageMap = new HashMap<String, HatchetImage>(); if (albums.images != null) { for (HatchetImage image : albums.images) { imageMap.put(image.id, image); } } Map<HatchetAlbumInfo, HatchetTracks> tracksMap = new HashMap<HatchetAlbumInfo, HatchetTracks>(); if (albumInfo.tracks != null && albumInfo.tracks.size() > 0) { params.clear(); for (String trackId : albumInfo.tracks) { params.put(HATCHET_PARAM_IDARRAY, trackId); } rawJsonString = TomahawkUtils .httpGet(buildQuery(InfoRequestData.INFOREQUESTDATA_TYPE_TRACKS, params)).mResponseText; HatchetTracks tracks = mObjectMapper.readValue(rawJsonString, HatchetTracks.class); tracksMap.put(albumInfo, tracks); } infoRequestData.setInfoResult(albumInfo); resultMapList.put(HATCHET_IMAGES, imageMap); resultMapList.put(HATCHET_TRACKS, tracksMap); } infoRequestData.setInfoResultMap(resultMapList); return true; } else if (infoRequestData.getType() == InfoRequestData.INFOREQUESTDATA_TYPE_SEARCHES) { rawJsonString = TomahawkUtils.httpGet(buildQuery(InfoRequestData.INFOREQUESTDATA_TYPE_SEARCHES, infoRequestData.getParams())).mResponseText; infoRequestData.setInfoResult(mObjectMapper.readValue(rawJsonString, HatchetSearch.class)); return true; } else if (infoRequestData.getType() == InfoRequestData.INFOREQUESTDATA_TYPE_RELATIONSHIPS_USERS_FOLLOWERS || infoRequestData .getType() == InfoRequestData.INFOREQUESTDATA_TYPE_RELATIONSHIPS_USERS_FOLLOWINGS) { rawJsonString = TomahawkUtils.httpGet(buildQuery(InfoRequestData.INFOREQUESTDATA_TYPE_RELATIONSHIPS, infoRequestData.getParams())).mResponseText; HatchetRelationshipsStruct relationshipsStruct = mObjectMapper.readValue(rawJsonString, HatchetRelationshipsStruct.class); for (HatchetRelationshipStruct relationship : relationshipsStruct.relationships) { String userId; if (infoRequestData .getType() == InfoRequestData.INFOREQUESTDATA_TYPE_RELATIONSHIPS_USERS_FOLLOWERS) { userId = relationship.user; } else { userId = relationship.targetUser; } params.put(HATCHET_PARAM_IDARRAY, userId); } rawJsonString = TomahawkUtils .httpGet(buildQuery(InfoRequestData.INFOREQUESTDATA_TYPE_USERS, params)).mResponseText; HatchetUsers hatchetUsers = mObjectMapper.readValue(rawJsonString, HatchetUsers.class); infoRequestData.setInfoResult(hatchetUsers); return true; } else if (infoRequestData .getType() == InfoRequestData.INFOREQUESTDATA_TYPE_RELATIONSHIPS_USERS_STARREDALBUMS || infoRequestData .getType() == InfoRequestData.INFOREQUESTDATA_TYPE_RELATIONSHIPS_USERS_STARREDARTISTS) { if (!infoRequestData.getParams().containsKey(HATCHET_PARAM_USERID)) { infoRequestData.getParams().put(HATCHET_PARAM_USERID, mUserId); } rawJsonString = TomahawkUtils.httpGet(buildQuery(InfoRequestData.INFOREQUESTDATA_TYPE_RELATIONSHIPS, infoRequestData.getParams())).mResponseText; HatchetRelationshipsStruct relationshipsStruct = mObjectMapper.readValue(rawJsonString, HatchetRelationshipsStruct.class); if (relationshipsStruct.relationships.isEmpty()) { return false; } infoRequestData.setInfoResult(relationshipsStruct); return true; } Log.d(TAG, "doInBackground(...) took " + (System.currentTimeMillis() - start) + "ms to finish"); return false; }