Java Code Examples for com.google.common.collect.Multimaps#newListMultimap()

The following examples show how to use com.google.common.collect.Multimaps#newListMultimap() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CycleDetectingLock.java    From businessworks with Apache License 2.0 5 votes vote down vote up
/**
 * Algorithm to detect a potential lock cycle.
 *
 * For lock's thread owner check which lock is it trying to take.
 * Repeat recursively. When current thread is found a potential cycle is detected.
 *
 * @see CycleDetectingLock#lockOrDetectPotentialLocksCycle()
 */
private ListMultimap<Long, ID> detectPotentialLocksCycle() {
  final long currentThreadId = Thread.currentThread().getId();
  if (lockOwnerThreadId == null || lockOwnerThreadId == currentThreadId) {
    // if nobody owns this lock, lock cycle is impossible
    // if a current thread owns this lock, we let Guice to handle it
    return ImmutableListMultimap.of();
  }

  ListMultimap<Long, ID> potentialLocksCycle = Multimaps.newListMultimap(
      new LinkedHashMap<Long, Collection<ID>>(),
      new Supplier<List<ID>>() {
        @Override
        public List<ID> get() {
          return Lists.newArrayList();
        }
      });
  // lock that is a part of a potential locks cycle, starts with current lock
  ReentrantCycleDetectingLock lockOwnerWaitingOn = this;
  // try to find a dependency path between lock's owner thread and a current thread
  while (lockOwnerWaitingOn != null && lockOwnerWaitingOn.lockOwnerThreadId != null) {
    Long threadOwnerThreadWaits = lockOwnerWaitingOn.lockOwnerThreadId;
    // in case locks cycle exists lock we're waiting for is part of it
    potentialLocksCycle.putAll(threadOwnerThreadWaits,
        getAllLockIdsAfter(threadOwnerThreadWaits, lockOwnerWaitingOn));

    if (threadOwnerThreadWaits == currentThreadId) {
      // owner thread depends on current thread, cycle detected
      return potentialLocksCycle;
    }
    // going for the next thread we wait on indirectly
    lockOwnerWaitingOn = lockThreadIsWaitingOn.get(threadOwnerThreadWaits);
  }
  // no dependency path from an owner thread to a current thread
  return ImmutableListMultimap.of();
}
 
Example 2
Source File: Multimaps2.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Creates a new, empty {@code ListMultimap} with the default initial capacities that uses a linked map internally.
 */
public static <K, V> ListMultimap<K, V> newLinkedHashListMultimap() {
	return Multimaps.newListMultimap(Maps.<K, Collection<V>> newLinkedHashMap(), new Supplier<List<V>>() {
		@Override
		public List<V> get() {
			return Lists.newArrayList();
		}
	});
}
 
Example 3
Source File: FlowDocumentation.java    From nomulus with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a FlowDocumentation for this flow class using data from javadoc tags.  Not public
 * because clients should get FlowDocumentation objects via the DocumentationGenerator class.
 */
protected FlowDocumentation(ClassDoc flowDoc) {
  name = flowDoc.name();
  qualifiedName = flowDoc.qualifiedName();
  packageName = flowDoc.containingPackage().name();
  classDocs = flowDoc.commentText();
  errors = new ArrayList<>();
  // Store error codes in sorted order, and leave reasons in insert order.
  errorsByCode =
      Multimaps.newListMultimap(new TreeMap<Long, Collection<ErrorCase>>(), ArrayList::new);
  parseTags(flowDoc);
}
 
Example 4
Source File: BashIncludeCommandImpl.java    From BashSupport with Apache License 2.0 5 votes vote down vote up
@Override
public boolean processDeclarations(@NotNull PsiScopeProcessor processor, @NotNull ResolveState state, PsiElement lastParent, @NotNull PsiElement place) {
    boolean result = PsiScopesUtil.walkChildrenScopes(this, processor, state, lastParent, place);
    if (!result) {
        //processing is done here
        return false;
    }

    //fixme right file?
    PsiFile containingFile = getContainingFile();
    PsiFile includedFile = BashPsiUtils.findIncludedFile(this);

    Multimap<VirtualFile, PsiElement> visitedFiles = state.get(visitedIncludeFiles);
    if (visitedFiles == null) {
        visitedFiles = Multimaps.newListMultimap(Maps.newHashMap(), Lists::newLinkedList);
    }

    visitedFiles.put(containingFile.getVirtualFile(), null);

    if (includedFile != null && !visitedFiles.containsKey(includedFile.getVirtualFile())) {
        //mark the file as visited before the actual visit, otherwise we'll get a stack overflow
        visitedFiles.put(includedFile.getVirtualFile(), this);

        state = state.put(visitedIncludeFiles, visitedFiles);

        return includedFile.processDeclarations(processor, state, null, place);
    }

    return true;
}
 
Example 5
Source File: StatementContextBase.java    From yangtools with Eclipse Public License 1.0 4 votes vote down vote up
private static <T> Multimap<ModelProcessingPhase, T> newMultimap() {
    return Multimaps.newListMultimap(new EnumMap<>(ModelProcessingPhase.class), () -> new ArrayList<>(1));
}
 
Example 6
Source File: Multimaps2.java    From xtext-extras with Eclipse Public License 2.0 3 votes vote down vote up
/**
 * Constructs an empty {@code ListMultimap} with enough capacity to hold the specified numbers of keys and values
 * without resizing. It uses a linked map internally.
 * 
 * @param expectedKeys
 *            the expected number of distinct keys
 * @param expectedValuesPerKey
 *            the expected average number of values per key
 * @throws IllegalArgumentException
 *             if {@code expectedKeys} or {@code expectedValuesPerKey} is negative
 */
public static <K, V> ListMultimap<K, V> newLinkedHashListMultimap(int expectedKeys, final int expectedValuesPerKey) {
	return Multimaps.newListMultimap(new LinkedHashMap<K, Collection<V>>(expectedKeys), new Supplier<List<V>>() {
		@Override
		public List<V> get() {
			return Lists.newArrayListWithCapacity(expectedValuesPerKey);
		}
	});
}