All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.parse.LockSet Maven / Gradle / Ivy

Go to download

A library that gives you access to the powerful Parse cloud platform from your Android app.

There is a newer version: 1.17.3
Show newest version
/*
 * Copyright (c) 2015-present, Parse, LLC.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 */
package com.parse;

import java.util.Collection;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;
import java.util.WeakHashMap;
import java.util.concurrent.locks.Lock;

/** package */ class LockSet {
  private static WeakHashMap stableIds = new WeakHashMap();
  private static long nextStableId = 0L;

  private final Set locks;

  public LockSet(Collection locks) {
    this.locks = new TreeSet(new Comparator() {
      @Override
      public int compare(Lock lhs, Lock rhs) {
        Long lhsId = getStableId(lhs);
        Long rhsId = getStableId(rhs);
        return lhsId.compareTo(rhsId);
      }
    });
    this.locks.addAll(locks);
  }

  private static Long getStableId(Lock lock) {
    synchronized (stableIds) {
      if (stableIds.containsKey(lock)) {
        return stableIds.get(lock);
      }
      long id = nextStableId++;
      stableIds.put(lock, id);
      return id;
    }
  }

  public void lock() {
    for (Lock l : locks) {
      l.lock();
    }
  }

  public void unlock() {
    for (Lock l : locks) {
      l.unlock();
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy