dmolony-DiskBrowser/src/com/bytezone/diskbrowser/infocom/PropertyTester.java

79 lines
2.7 KiB
Java
Raw Normal View History

2015-06-01 09:35:51 +00:00
package com.bytezone.diskbrowser.infocom;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import com.bytezone.diskbrowser.infocom.ZObject.Property;
2020-02-09 13:02:48 +00:00
// -----------------------------------------------------------------------------------//
2015-06-01 09:35:51 +00:00
class PropertyTester implements Iterable<Integer>
2020-02-09 13:02:48 +00:00
// -----------------------------------------------------------------------------------//
2015-06-01 09:35:51 +00:00
{
2020-02-02 10:17:49 +00:00
List<ZObject> objects;
List<Condition> conditions = new ArrayList<> ();
List<Integer> matchedProperties;
2015-06-01 09:35:51 +00:00
2020-02-09 13:02:48 +00:00
// ---------------------------------------------------------------------------------//
PropertyTester (List<ZObject> objects)
// ---------------------------------------------------------------------------------//
2020-02-02 10:17:49 +00:00
{
this.objects = objects;
}
2015-06-01 09:35:51 +00:00
2020-02-09 13:02:48 +00:00
// ---------------------------------------------------------------------------------//
2020-02-02 10:17:49 +00:00
public void addTest (Condition test)
2020-02-09 13:02:48 +00:00
// ---------------------------------------------------------------------------------//
2020-02-02 10:17:49 +00:00
{
conditions.add (test);
}
2015-06-01 09:35:51 +00:00
2020-02-09 13:02:48 +00:00
// ---------------------------------------------------------------------------------//
2020-02-02 10:17:49 +00:00
public void doTests ()
2020-02-09 13:02:48 +00:00
// ---------------------------------------------------------------------------------//
2020-02-02 10:17:49 +00:00
{
boolean[] propFail = new boolean[32];
int[] propTestCount = new int[32];
matchedProperties = new ArrayList<> ();
2015-06-01 09:35:51 +00:00
2020-02-02 10:17:49 +00:00
for (ZObject object : objects)
propertyLoop: for (Property property : object.properties)
{
if (propFail[property.propertyNumber] || property.length == 0)
continue;
for (Condition condition : conditions)
if (!condition.test (property))
{
propFail[property.propertyNumber] = true;
continue propertyLoop;
}
++propTestCount[property.propertyNumber];
}
2015-06-01 09:35:51 +00:00
2020-02-02 10:17:49 +00:00
for (int i = 1; i < propFail.length; i++)
if (!propFail[i] && propTestCount[i] > 0)
matchedProperties.add (i);
}
2015-06-01 09:35:51 +00:00
2020-02-09 13:02:48 +00:00
// ---------------------------------------------------------------------------------//
2020-02-02 10:17:49 +00:00
@Override
public Iterator<Integer> iterator ()
2020-02-09 13:02:48 +00:00
// ---------------------------------------------------------------------------------//
2020-02-02 10:17:49 +00:00
{
return matchedProperties.iterator ();
}
2015-06-01 09:35:51 +00:00
2020-02-09 13:02:48 +00:00
// ---------------------------------------------------------------------------------//
2020-02-02 10:17:49 +00:00
public int totalSuccessfulProperties ()
2020-02-09 13:02:48 +00:00
// ---------------------------------------------------------------------------------//
2020-02-02 10:17:49 +00:00
{
return matchedProperties.size ();
}
2015-06-01 09:35:51 +00:00
}
2020-02-09 13:02:48 +00:00
// ---------------------------------------------------------------------------------//
2015-06-01 09:35:51 +00:00
abstract class Condition
2020-02-09 13:02:48 +00:00
// ---------------------------------------------------------------------------------//
2015-06-01 09:35:51 +00:00
{
2020-02-02 10:17:49 +00:00
abstract boolean test (Property property);
2015-06-01 09:35:51 +00:00
}