001package net.tnemc.item.providers;
002/*
003 * The New Economy
004 * Copyright (C) 2025 Daniel "creatorfromhell" Vidmar
005 *
006 * This program is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU Affero General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * This program is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU Affero General Public License for more details.
015 *
016 * You should have received a copy of the GNU Affero General Public License
017 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
018 */
019
020import net.kyori.adventure.text.Component;
021import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
022
023import java.util.HashSet;
024import java.util.LinkedList;
025import java.util.List;
026import java.util.Set;
027
028/**
029 * Util
030 *
031 * @author creatorfromhell
032 * @since 1.0.0.0
033 */
034public class Util {
035
036  /**
037   * Compares the text content of two lists of Components to determine if they are equal.
038   *
039   * @param list1 the first list of Components to compare
040   * @param list2 the second list of Components to compare
041   * @return true if the text content of the two lists is equal, false otherwise
042   * @since 0.2.0.0
043   * @author creatorfromhell
044   */
045  public static boolean textComponentsEqual(final List<Component> list1, final List<Component> list2) {
046    final LinkedList<String> list1Copy = new LinkedList<>();
047    for(final Component component : list1) {
048      list1Copy.add(PlainTextComponentSerializer.plainText().serialize(component));
049    }
050
051    final LinkedList<String> list2Copy = new LinkedList<>();
052    for(final Component component : list2) {
053      list2Copy.add(PlainTextComponentSerializer.plainText().serialize(component));
054    }
055    return listsEquals(list1Copy, list2Copy);
056  }
057
058  /**
059   * Compares two lists for equality regardless of order.
060   *
061   * @param list1 the first list to be compared
062   * @param list2 the second list to be compared
063   * @return true if the two lists contain the same elements, false otherwise
064   * @since 0.2.0.0
065   * @author creatorfromhell
066   */
067  public static <V> boolean listsEquals(final List<V> list1, final List<V> list2) {
068    return new HashSet<>(list1).containsAll(list2) && new HashSet<>(list2).containsAll(list1);
069  }
070
071  /**
072   * Compares two lists to check if they contain the same elements.
073   *
074   * @param list1 the first list to compare
075   * @param list2 the second list to compare
076   * @param debug true if debug information should be printed, false otherwise
077   * @param <V> the type of elements in the lists
078   * @return true if the lists contain the same elements, false otherwise
079   * @since 0.2.0.0
080   * @author creatorfromhell
081   */
082  public static <V> boolean listsEquals(final List<V> list1, final List<V> list2, final boolean debug) {
083
084    if(debug) {
085
086      System.out.println("List 1");
087      for(final V item : list1) {
088        System.out.println("Item: " + item);
089      }
090
091      System.out.println("List 2");
092      for(final V item : list2) {
093        System.out.println("Item: " + item);
094      }
095    }
096
097    return new HashSet<>(list1).containsAll(list2) && new HashSet<>(list2).containsAll(list1);
098  }
099
100  /**
101   * Compares two sets to see if they are equal. The sets are considered equal if each set contains all elements of the other set.
102   *
103   * @param list1 the first set to be compared
104   * @param list2 the second set to be compared
105   * @return true if the sets are equal, false otherwise
106   * @since 0.2.0.0
107   * @author creatorfromhell
108   */
109  public static <V> boolean setsEquals(final Set<V> list1, final Set<V> list2) {
110    return new HashSet<>(list1).containsAll(list2) && new HashSet<>(list2).containsAll(list1);
111  }
112}