<commit_before>/*
 * Cacheonix Systems licenses this file to You under the LGPL 2.1
 * (the "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *      http://www.cacheonix.org/products/cacheonix/license-lgpl-2.1.htm
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.cacheonix.impl.lock;

import java.util.LinkedList;

import org.cacheonix.impl.net.cluster.ClusterProcessor;
import org.cacheonix.impl.util.Assert;
import org.cacheonix.impl.util.logging.Logger;

/**
 * A functor that grants next pending lock requests.
 */
@SuppressWarnings("RedundantIfStatement")
public final class NextLockRequestGranter {

   /**
    * Logger.
    *
    * @noinspection UNUSED_SYMBOL, UnusedDeclaration
    */
   private static final Logger LOG = Logger.getLogger(NextLockRequestGranter.class); // NOPMD

   private final ClusterProcessor clusterProcessor;

   private final LockQueue lockQueue;


   public NextLockRequestGranter(final ClusterProcessor clusterProcessor, final LockQueue lockQueue) {

      this.clusterProcessor = clusterProcessor;
      this.lockQueue = lockQueue;
   }


   /**
    * Grants next lock request(s) while possible. The granter is capable of detecting if there is work to do.
    */
   public void grantNextLockRequests() {

      // Grant next lock request(s) while possible.
      do {
      } while (grantNextLockRequest());
   }


   /**
    * Tries to grant a next lock request.
    *
    * @return <code>true</code> if granted the next lock request. <code>false</code> if couldn't
    * @see #grantNextLockRequests()
    */
   private boolean grantNextLockRequest() {

      // Check if there are any requests
      final LinkedList<AcquireLockRequest> pendingRequests = lockQueue.getPendingRequests();
      if (pendingRequests.isEmpty()) {
         return false;
      }

      // Try to grant
      final AcquireLockRequest acquireLockRequest = pendingRequests.peek();

      if (acquireLockRequest.isReadLock()) {

         // Try to grant a read lock

         final LockOwner writeLockOwner = lockQueue.getWriteLockOwner();
         if (writeLockOwner == null) {<commit_msg>Suppressed inspection 'Statement With Empty Body'<commit_after>/*
 * Cacheonix Systems licenses this file to You under the LGPL 2.1
 * (the "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *      http://www.cacheonix.org/products/cacheonix/license-lgpl-2.1.htm
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.cacheonix.impl.lock;

import java.util.LinkedList;

import org.cacheonix.impl.net.cluster.ClusterProcessor;
import org.cacheonix.impl.util.Assert;
import org.cacheonix.impl.util.logging.Logger;

/**
 * A functor that grants next pending lock requests.
 */
@SuppressWarnings("RedundantIfStatement")
public final class NextLockRequestGranter {

   /**
    * Logger.
    *
    * @noinspection UNUSED_SYMBOL, UnusedDeclaration
    */
   private static final Logger LOG = Logger.getLogger(NextLockRequestGranter.class); // NOPMD

   private final ClusterProcessor clusterProcessor;

   private final LockQueue lockQueue;


   public NextLockRequestGranter(final ClusterProcessor clusterProcessor, final LockQueue lockQueue) {

      this.clusterProcessor = clusterProcessor;
      this.lockQueue = lockQueue;
   }


   /**
    * Grants next lock request(s) while possible. The granter is capable of detecting if there is work to do.
    */
   public void grantNextLockRequests() {

      // Grant next lock request(s) while possible.
      //noinspection StatementWithEmptyBody
      while (grantNextLockRequest()) {
      }
   }


   /**
    * Tries to grant a next lock request.
    *
    * @return <code>true</code> if granted the next lock request. <code>false</code> if couldn't
    * @see #grantNextLockRequests()
    */
   private boolean grantNextLockRequest() {

      // Check if there are any requests
      final LinkedList<AcquireLockRequest> pendingRequests = lockQueue.getPendingRequests();
      if (pendingRequests.isEmpty()) {
         return false;
      }

      // Try to grant
      final AcquireLockRequest acquireLockRequest = pendingRequests.peek();

      if (acquireLockRequest.isReadLock()) {

         // Try to grant a read lock

         final LockOwner writeLockOwner = lockQueue.getWriteLockOwner();
         if (writeLockOwner == null) {
{'fraction_non_alphanumeric': 0.08483033932135728, 'fraction_numerical': 0.0015968063872255488, 'mean_word_length': 3.3161068044788977, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '10690', 'n_tokens_mistral': 1369, 'n_tokens_neox': 1339, 'n_words': 456}