/* * AppleCommander - An Apple ][ image utility. * Copyright (C) 2021-2022 by Robert Greene and others * robgreene at users.sourceforge.net * * This program 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 2 of the License, or (at your * option) any later version. * * This program 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, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ package com.webcodepro.applecommander.storage.compare; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Objects; import java.util.function.BiConsumer; import com.webcodepro.applecommander.storage.Disk; import com.webcodepro.applecommander.storage.DiskGeometry; import com.webcodepro.applecommander.storage.DiskUnrecognizedException; import com.webcodepro.applecommander.storage.FormattedDisk; import com.webcodepro.applecommander.storage.physical.ImageOrder; /** * Perform a disk comparison based on selected strategy. */ public class DiskDiff { public static ComparisonResult compare(Disk diskA, Disk diskB) { return new DiskDiff(diskA, diskB).compare(); } public static Builder create(Disk diskA, Disk diskB) { return new Builder(diskA, diskB); } private Disk diskA; private Disk diskB; private ComparisonResult results = new ComparisonResult(); private BiConsumer diskComparisonStrategy = this::compareByNativeGeometry; private DiskDiff(Disk diskA, Disk diskB) { Objects.requireNonNull(diskA); Objects.requireNonNull(diskB); this.diskA = diskA; this.diskB = diskB; } public ComparisonResult compare() { FormattedDisk[] formattedDisksA = null; try { formattedDisksA = diskA.getFormattedDisks(); } catch (DiskUnrecognizedException e) { results.addError(e); } FormattedDisk[] formattedDisksB = null; try { formattedDisksB = diskB.getFormattedDisks(); } catch (DiskUnrecognizedException e) { results.addError(e); } if (!results.hasErrors()) { compareAll(formattedDisksA, formattedDisksB); } return results; } public void compareAll(FormattedDisk[] formattedDisksA, FormattedDisk[] formattedDisksB) { Objects.requireNonNull(formattedDisksA); Objects.requireNonNull(formattedDisksB); if (formattedDisksA.length != formattedDisksB.length) { results.addWarning("Cannot compare all disks; %s has %d while %s has %d.", diskA.getFilename(), formattedDisksA.length, diskB.getFilename(), formattedDisksB.length); } int min = Math.min(formattedDisksA.length, formattedDisksB.length); for (int i=0; i %d)", orderA.getBlocksOnDevice(), orderB.getBlocksOnDevice()); return; } for (int block=0; block %d)", orderA.getSectorsPerDisk(), orderB.getSectorsPerDisk()); return; } for (int track=0; track unequalSectors = new ArrayList<>(); for (int sector=0; sector