/**************************************************************************
OmegaT - Computer Assisted Translation (CAT) tool
with fuzzy matching, translation memory, keyword search,
glossaries, and translation leveraging into updated projects.
Copyright (C) 2016 Aaron Madlon-Kay
Home page: http://www.omegat.org/
Support center: http://groups.yahoo.com/group/OmegaT/
This file is part of OmegaT.
OmegaT is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OmegaT is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
**************************************************************************/
package org.omegat.filters2.master;
import java.util.List;
import java.util.Objects;
import gen.core.filters.Files;
import gen.core.filters.Filter;
import gen.core.filters.Filter.Option;
import gen.core.filters.Filters;
/**
* Utilities for {@link Filters} and associated objects
*
* @author Aaron Madlon-Kay
*/
public class FiltersUtil {
private FiltersUtil() {
}
/**
* An implementation of {@link Object#equals(Object)} for {@link Filters} objects, which are generated by
* JAXB and thus don't have this method and shouldn't be manually modified. (It is apparently possible to
* have this generated, but I didn't look too hard into it.).
*
* @return
*/
public static boolean filtersEqual(Filters o1, Filters o2) {
if (o1 == o2) {
return true;
}
return o1.isIgnoreFileContext() == o2.isIgnoreFileContext()
&& o1.isPreserveSpaces() == o2.isPreserveSpaces()
&& o1.isRemoveSpacesNonseg() == o2.isRemoveSpacesNonseg()
&& o1.isRemoveTags() == o2.isRemoveTags()
&& filterListsEqual(o1.getFilters(), o2.getFilters());
}
private static boolean filterListsEqual(List<Filter> filters, List<Filter> filters2) {
if (filters == filters2) {
return true;
}
if (filters.size() != filters2.size()) {
return false;
}
for (int i = 0; i < filters.size(); i++) {
if (!filterObjsEqual(filters.get(i), filters2.get(i))) {
return false;
}
}
return true;
}
private static boolean filterObjsEqual(Filter filter, Filter filter2) {
if (filter == filter2) {
return true;
}
return filter.isEnabled() == filter2.isEnabled()
&& Objects.equals(filter.getClassName(), filter2.getClassName())
&& filesEqual(filter.getFiles(), filter2.getFiles())
&& optionsEqual(filter.getOption(), filter2.getOption());
}
private static boolean optionsEqual(List<Option> option, List<Option> option2) {
if (option == option2) {
return true;
}
if (option.size() != option2.size()) {
return false;
}
for (int i = 0; i < option.size(); i++) {
if (!optionObjsEqual(option.get(i), option2.get(i))) {
return false;
}
}
return true;
}
private static boolean optionObjsEqual(Option option, Option option2) {
if (option == option2) {
return true;
}
return Objects.equals(option.getName(), option2.getName())
&& Objects.equals(option.getValue(), option2.getValue());
}
private static boolean filesEqual(List<Files> files, List<Files> files2) {
if (files == files2) {
return true;
}
if (files.size() != files2.size()) {
return false;
}
for (int i = 0; i < files.size(); i++) {
if (!fileObjsEqual(files.get(i), files2.get(i))) {
return false;
}
}
return true;
}
private static boolean fileObjsEqual(Files files, Files files2) {
if (files == files2) {
return true;
}
return Objects.equals(files.getSourceEncoding(), files2.getSourceEncoding())
&& Objects.equals(files.getSourceFilenameMask(), files2.getSourceFilenameMask())
&& Objects.equals(files.getTargetEncoding(), files2.getTargetEncoding())
&& Objects.equals(files.getTargetFilenamePattern(), files2.getTargetFilenamePattern());
}
}