<commit_before>/*
 * Copyright 2000-2009 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
 *
 * 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 com.intellij.notification.impl;

import com.intellij.notification.Notification;
import com.intellij.notification.NotificationType;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Pair;
import com.intellij.util.PairFunction;
import com.intellij.util.concurrency.ReentrantLock2;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.lang.reflect.Array;
import java.util.*;

/**
 * @author spleaner
 */
public class NotificationModel {

  private final Map<Notification, Pair<Project, Boolean>> myNotifications = new LinkedHashMap<Notification, Pair<Project, Boolean>>();
  private final List<NotificationModelListener> myListeners = ContainerUtil.createEmptyCOWList();

  private final ReentrantLock2 myLock = new ReentrantLock2();

  public void addListener(@NotNull final NotificationModelListener listener) {
    myListeners.add(listener);
  }

  public void removeListener(@NotNull final NotificationModelListener listener) {
    myListeners.remove(listener);
  }

  public void add(@NotNull final Notification notification, final @Nullable Project project) {
    try {
      myLock.acquire();
      myNotifications.put(notification, Pair.create(project, false));
    }
    finally {
      myLock.release();
    }

    for (NotificationModelListener listener : myListeners) {
      listener.notificationsAdded(notification);
    }

  }

  public void markRead() {
    List<Notification> changed = null;

    try {
      myLock.acquire();
      if (myNotifications.isEmpty()) return;

      changed = new ArrayList<Notification>();

      for (final Map.Entry<Notification, Pair<Project, Boolean>> entry : myNotifications.entrySet()) {
        entry.setValue(new Pair<Project, Boolean>(entry.getValue().first, true));
        changed.add(entry.getKey());
      }
    }
    finally {
      myLock.release();
    }

    if (!changed.isEmpty()) {
      final Notification[] read = changed.toArray(new Notification[changed.size()]);
      for (final NotificationModelListener listener : myListeners) {
        listener.notificationsRead(read);
      }
    }
  }

  @Nullable
  public Notification remove(@NotNull final Notification notification) {
    try {
      myLock.acquire();
      final Pair<Project, Boolean> pair = myNotifications.remove(notification);
      if (pair == null) return notification;
    }
    finally {
      myLock.release();
    }

    for (NotificationModelListener listener : myListeners) {
      listener.notificationsRemoved(notification);
    }

    return notification;
  }

  public void remove(@NotNull final Notification... notifications) {
    final List<Notification> tbr = new ArrayList<Notification>();
    try {
      myLock.acquire();
      for (final Notification notification : notifications) {
        final Pair<Project, Boolean> pair = myNotifications.remove(notification);
        if (pair != null) {
          tbr.add(notification);
        }
      }
    }
    finally {
      myLock.release();
    }

    if (!tbr.isEmpty()) {
      final Notification[] removed = tbr.toArray((Notification[])Array.newInstance(tbr.get(0).getClass(), tbr.size()));
      for (NotificationModelListener listener : myListeners) {
        listener.notificationsRemoved(removed);
      }
    }
  }<commit_msg>[19576] ASE: NotificationModel.remove<commit_after>/*
 * Copyright 2000-2009 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
 *
 * 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 com.intellij.notification.impl;

import com.intellij.notification.Notification;
import com.intellij.notification.NotificationType;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Pair;
import com.intellij.util.PairFunction;
import com.intellij.util.concurrency.ReentrantLock2;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.*;

/**
 * @author spleaner
 */
public class NotificationModel {

  private final Map<Notification, Pair<Project, Boolean>> myNotifications = new LinkedHashMap<Notification, Pair<Project, Boolean>>();
  private final List<NotificationModelListener> myListeners = ContainerUtil.createEmptyCOWList();

  private final ReentrantLock2 myLock = new ReentrantLock2();

  public void addListener(@NotNull final NotificationModelListener listener) {
    myListeners.add(listener);
  }

  public void removeListener(@NotNull final NotificationModelListener listener) {
    myListeners.remove(listener);
  }

  public void add(@NotNull final Notification notification, final @Nullable Project project) {
    try {
      myLock.acquire();
      myNotifications.put(notification, Pair.create(project, false));
    }
    finally {
      myLock.release();
    }

    for (NotificationModelListener listener : myListeners) {
      listener.notificationsAdded(notification);
    }

  }

  public void markRead() {
    List<Notification> changed = null;

    try {
      myLock.acquire();
      if (myNotifications.isEmpty()) return;

      changed = new ArrayList<Notification>();

      for (final Map.Entry<Notification, Pair<Project, Boolean>> entry : myNotifications.entrySet()) {
        entry.setValue(new Pair<Project, Boolean>(entry.getValue().first, true));
        changed.add(entry.getKey());
      }
    }
    finally {
      myLock.release();
    }

    if (!changed.isEmpty()) {
      final Notification[] read = changed.toArray(new Notification[changed.size()]);
      for (final NotificationModelListener listener : myListeners) {
        listener.notificationsRead(read);
      }
    }
  }

  @Nullable
  public Notification remove(@NotNull final Notification notification) {
    try {
      myLock.acquire();
      final Pair<Project, Boolean> pair = myNotifications.remove(notification);
      if (pair == null) return notification;
    }
    finally {
      myLock.release();
    }

    for (NotificationModelListener listener : myListeners) {
      listener.notificationsRemoved(notification);
    }

    return notification;
  }

  public void remove(@NotNull final Notification... notifications) {
    final List<Notification> tbr = new ArrayList<Notification>();
    try {
      myLock.acquire();
      for (final Notification notification : notifications) {
        final Pair<Project, Boolean> pair = myNotifications.remove(notification);
        if (pair != null) {
          tbr.add(notification);
        }
      }
    }
    finally {
      myLock.release();
    }

    if (!tbr.isEmpty()) {
      final Notification[] removed = tbr.toArray(new Notification[tbr.size()]);
      for (NotificationModelListener listener : myListeners) {
        listener.notificationsRemoved(removed);
      }
    }
  }
{'fraction_non_alphanumeric': 0.10459828196058615, 'fraction_numerical': 0.004547751389590703, 'mean_word_length': 3.6190198366394397, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, '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': '7395062', 'n_tokens_mistral': 2166, 'n_tokens_neox': 2095, 'n_words': 558}