List of usage examples for java.util.concurrent.locks Lock lock
lock
From source file:com.netprogs.minecraft.plugins.social.SocialPerson.java
public void removeRelationship(SocialPerson memberPerson) { Lock lock = rwRelationshipLock.writeLock(); lock.lock(); try {//from w ww .ja v a 2 s . c o m String memberName = memberPerson.getName(); firePlayerMemberChangeEvent(memberName, SocialNetworkCommandType.relationship, Type.preRemove, (person.getRelationships().size() == 0)); person.getRelationships().remove(memberName); relationships.remove(memberName); firePlayerMemberChangeEvent(memberName, SocialNetworkCommandType.relationship, Type.postRemove, (person.getRelationships().size() == 0)); } finally { lock.unlock(); } }
From source file:net.myrrix.online.ServerRecommender.java
/** * <p>Lists the items that were most influential in recommending a given item to a given user. Exactly how this * is determined is left to the implementation, but, generally this will return items that the user prefers * and that are similar to the given item.</p> * * <p>These values by which the results are ordered are opaque values and have no interpretation * other than that larger means stronger.</p> * * @param userID ID of user who was recommended the item * @param itemID ID of item that was recommended * @param howMany maximum number of items to return * @return {@link List} of {@link RecommendedItem}, ordered from most influential in recommended the given * item to least/*from w w w. j av a 2 s . co m*/ * @throws NoSuchUserException if the user is not known in the model * @throws NoSuchItemException if the item is not known in the model * @throws NotReadyException if the recommender has no model available yet */ @Override public List<RecommendedItem> recommendedBecause(long userID, long itemID, int howMany) throws NoSuchUserException, NoSuchItemException, NotReadyException { Preconditions.checkArgument(howMany > 0, "howMany must be positive"); Generation generation = getCurrentGeneration(); FastByIDMap<FastIDSet> knownItemIDs = generation.getKnownItemIDs(); if (knownItemIDs == null) { throw new UnsupportedOperationException("No known item IDs available"); } Lock knownItemLock = generation.getKnownItemLock().readLock(); FastIDSet userKnownItemIDs; knownItemLock.lock(); try { userKnownItemIDs = knownItemIDs.get(userID); } finally { knownItemLock.unlock(); } if (userKnownItemIDs == null) { throw new NoSuchUserException(userID); } FastByIDMap<float[]> Y = generation.getY(); Lock yLock = generation.getYLock().readLock(); yLock.lock(); try { float[] features = Y.get(itemID); if (features == null) { throw new NoSuchItemException(itemID); } FastByIDMap<float[]> toFeatures; synchronized (userKnownItemIDs) { toFeatures = new FastByIDMap<float[]>(userKnownItemIDs.size()); LongPrimitiveIterator it = userKnownItemIDs.iterator(); while (it.hasNext()) { long fromItemID = it.nextLong(); float[] fromFeatures = Y.get(fromItemID); toFeatures.put(fromItemID, fromFeatures); } } return TopN.selectTopN(new RecommendedBecauseIterator(toFeatures.entrySet().iterator(), generation.getUserTagIDs(), features), howMany); } finally { yLock.unlock(); } }
From source file:com.netprogs.minecraft.plugins.social.SocialPerson.java
public void breakMarriage() { Lock lock = rwMarriageLock.writeLock(); lock.lock(); try {/* www .j a v a2s . c om*/ String spouseName = StringUtils.EMPTY; if (person.getMarriage() != null) { spouseName = person.getMarriage().getPlayerName(); firePlayerMemberChangeEvent(spouseName, SocialNetworkCommandType.marriage, Type.preRemove, true); } person.setMarriage(null); socialMarriage = null; if (StringUtils.isNotEmpty(spouseName)) { firePlayerMemberChangeEvent(spouseName, SocialNetworkCommandType.marriage, Type.postRemove, true); } } finally { lock.unlock(); } }
From source file:com.netprogs.minecraft.plugins.social.SocialPerson.java
public <U extends IMessage> int getMessageCountFrom(String fromPlayerName, Class<U> messageClass) { Lock lock = rwMessageQueueLock.readLock(); lock.lock(); try {//from www . j a va2 s . c o m String className = messageClass.getCanonicalName(); if (person.getMessageQueue().containsKey(className)) { if (person.getMessageQueue().get(className).containsKey(fromPlayerName)) { return person.getMessageQueue().get(className).get(fromPlayerName).size(); } } return 0; } finally { lock.unlock(); } }
From source file:com.netprogs.minecraft.plugins.social.SocialPerson.java
public <U extends IMessage> int getMessagesCount(Class<U> messageClass) { Lock lock = rwMessageQueueLock.readLock(); lock.lock(); try {// w ww . j a va2 s. c om int count = 0; String className = messageClass.getCanonicalName(); if (person.getMessageQueue().containsKey(className)) { Set<String> senders = person.getMessageQueue().get(className).keySet(); for (String sender : senders) { count += person.getMessageQueue().get(className).get(sender).size(); } } return count; } finally { lock.unlock(); } }
From source file:com.netprogs.minecraft.plugins.social.SocialPerson.java
public void breakEngagement() { Lock lock = rwEngagementLock.writeLock(); lock.lock(); try {/*from w w w .j a va2 s . co m*/ String engagementName = StringUtils.EMPTY; if (person.getEngagement() != null) { engagementName = person.getEngagement().getPlayerName(); firePlayerMemberChangeEvent(engagementName, SocialNetworkCommandType.engagement, Type.preRemove, true); } person.setEngagement(null); socialEngagement = null; if (StringUtils.isNotEmpty(engagementName)) { firePlayerMemberChangeEvent(engagementName, SocialNetworkCommandType.engagement, Type.postRemove, true); } } finally { lock.unlock(); } }
From source file:com.netprogs.minecraft.plugins.social.SocialPerson.java
/** * Returns all the messages of the given type. * @param messageClass//from w ww . ja v a 2 s . co m * @return */ @SuppressWarnings("unchecked") public <U extends IMessage> Map<String, List<U>> getMessages(Class<U> messageClass) { Lock lock = rwMessageQueueLock.readLock(); lock.lock(); try { String className = messageClass.getCanonicalName(); if (person.getMessageQueue().containsKey(className)) { Map<String, List<U>> map = new HashMap<String, List<U>>( (Map<? extends String, ? extends List<U>>) person.getMessageQueue().get(className)); return Collections.unmodifiableMap(map); } // return an empty list return Collections.emptyMap(); } finally { lock.unlock(); } }
From source file:com.netprogs.minecraft.plugins.social.SocialPerson.java
/** * Removes all the messages of every type of the given person. * @param fromPlayerName// ww w . j a v a 2 s.c o m * @param messageClass * @return */ public void removeMessagesFrom(SocialPerson fromPerson) { Lock lock = rwMessageQueueLock.writeLock(); lock.lock(); try { // go through every class in the message queue and remove the player list from it for (String className : person.getMessageQueue().keySet()) { // remove the person person.getMessageQueue().get(className).remove(fromPerson.getName()); // if the message type list is empty, remove it also, just to save object memory if (person.getMessageQueue().get(className).keySet().size() == 0) { person.getMessageQueue().remove(className); } } } finally { lock.unlock(); } }
From source file:org.unitime.timetable.solver.exam.ExamSolver.java
public ExamProposedChange update(ExamProposedChange change) { Lock lock = currentSolution().getLock().writeLock(); lock.lock(); try {//from w w w. ja v a 2 s . c o m Hashtable<Exam, ExamPlacement> undoAssign = new Hashtable(); HashSet<Exam> undoUnassing = new HashSet(); Vector<Exam> unassign = new Vector(); Vector<ExamPlacement> assign = new Vector(); HashSet<ExamPlacement> conflicts = new HashSet(); /* for (ExamAssignment assignment: change.getConflicts()) { ExamPlacement placement = getPlacement(assignment); if (placement==null || !placement.equals(placement.variable().getAssignment())) return null; undoAssign.put((Exam)placement.variable(),placement); unassign.add((Exam)placement.variable()); } */ for (ExamAssignment assignment : change.getAssignments()) { ExamPlacement placement = getPlacement(assignment); if (placement == null) return null; if (currentSolution().getAssignment().getValue(placement.variable()) != null) undoAssign.put((Exam) placement.variable(), currentSolution().getAssignment().getValue(placement.variable())); else undoUnassing.add((Exam) placement.variable()); assign.add(placement); unassign.add((Exam) placement.variable()); } for (Exam exam : unassign) currentSolution().getAssignment().unassign(0, exam); for (ExamPlacement placement : assign) { for (Iterator i = placement.variable().getModel() .conflictValues(currentSolution().getAssignment(), placement).iterator(); i.hasNext();) { ExamPlacement conflict = (ExamPlacement) i.next(); Exam conflictingExam = (Exam) conflict.variable(); if (!undoAssign.containsKey(conflictingExam) && !undoUnassing.contains(conflictingExam)) undoAssign.put(conflictingExam, conflict); conflicts.add(conflict); currentSolution().getAssignment().unassign(0, conflict.variable()); } currentSolution().getAssignment().assign(0, placement); } change.getAssignments().clear(); change.getConflicts().clear(); for (ExamPlacement assignment : assign) if (!conflicts.contains(assignment)) change.getAssignments() .add(new ExamAssignmentInfo(assignment, currentSolution().getAssignment())); for (Exam exam : undoUnassing) if (currentSolution().getAssignment().getValue(exam) != null) currentSolution().getAssignment().unassign(0, exam); for (Map.Entry<Exam, ExamPlacement> entry : undoAssign.entrySet()) currentSolution().getAssignment().unassign(0, entry.getKey()); for (Map.Entry<Exam, ExamPlacement> entry : undoAssign.entrySet()) currentSolution().getAssignment().assign(0, entry.getValue()); for (ExamPlacement conflict : conflicts) { ExamPlacement original = undoAssign.get((Exam) conflict.variable()); change.getConflicts().add(new ExamAssignment(original == null ? conflict : original, currentSolution().getAssignment())); } return change; } finally { lock.unlock(); } }
From source file:org.unitime.timetable.solver.exam.ExamSolver.java
public boolean backup(File folder, String puid) { folder.mkdirs();/*from www . ja v a 2s.c o m*/ if (currentSolution() == null) return false; Lock lock = currentSolution().getLock().readLock(); lock.lock(); try { File outXmlFile = new File(folder, "exam_" + puid + BackupFileFilter.sXmlExtension); File outPropertiesFile = new File(folder, "exam_" + puid + BackupFileFilter.sPropertiesExtension); try { getProperties().setProperty("Xml.SaveConflictTable", "false"); FileOutputStream fos = null; try { fos = new FileOutputStream(outXmlFile); Document document = ((ExamModel) currentSolution().getModel()) .save(currentSolution().getAssignment()); ExamConflictStatisticsInfo cbsInfo = getCbsInfo(); if (cbsInfo != null) cbsInfo.save(document.getRootElement().addElement("cbsInfo")); (new XMLWriter(fos, OutputFormat.createPrettyPrint())).write(document); fos.flush(); fos.close(); fos = null; } finally { try { if (fos != null) fos.close(); } catch (IOException e) { } } for (Iterator i = getProperties().entrySet().iterator(); i.hasNext();) { Map.Entry entry = (Map.Entry) i.next(); if (!(entry.getKey() instanceof String)) { sLog.error("Configuration key " + entry.getKey() + " is not of type String (" + entry.getKey().getClass() + ")"); i.remove(); } else if (!(entry.getValue() instanceof String)) { sLog.error("Value of configuration key " + entry.getKey() + " is not of type String (" + entry.getValue() + " is of type " + entry.getValue().getClass() + ")"); i.remove(); } } try { fos = new FileOutputStream(outPropertiesFile); getProperties().store(fos, "Backup file"); fos.flush(); fos.close(); fos = null; } finally { try { if (fos != null) fos.close(); } catch (IOException e) { } } return true; } catch (Exception e) { sLog.error(e.getMessage(), e); if (outXmlFile.exists()) outXmlFile.delete(); if (outPropertiesFile.exists()) outPropertiesFile.delete(); } } finally { lock.unlock(); } return false; }