Enrico Weigelt, metux IT consult
2017-01-25 08:59:22 UTC
Dynamic method invocation isn't just sloooow (especially on that hot path!),
but also defeats (compile time) type safety.
The new interface isn't really required - we could directly check for
BuildingType directly. OTOH, having an interface gives us an bit of
semantic decoupling from Scope.
---
src/net/sf/freecol/common/model/BuildingType.java | 2 +-
src/net/sf/freecol/common/model/Scope.java | 7 ++++---
.../freecol/common/model/SpecObjectTypeHolder.java | 24 ++++++++++++++++++++++
3 files changed, 29 insertions(+), 4 deletions(-)
create mode 100644 src/net/sf/freecol/common/model/SpecObjectTypeHolder.java
diff --git a/src/net/sf/freecol/common/model/BuildingType.java b/src/net/sf/freecol/common/model/BuildingType.java
index 9c6556fbde9..d8ab71471d6 100644
--- a/src/net/sf/freecol/common/model/BuildingType.java
+++ b/src/net/sf/freecol/common/model/BuildingType.java
@@ -40,7 +40,7 @@ import static net.sf.freecol.common.util.CollectionUtils.*;
* of goods it produces and consumes.
*/
public final class BuildingType extends BuildableType
- implements BaseProduction {
+ implements BaseProduction, SpecObjectTypeHolder {
public static final String TAG = "building-type";
diff --git a/src/net/sf/freecol/common/model/Scope.java b/src/net/sf/freecol/common/model/Scope.java
index 7f6f57b1f9f..98c43f0322c 100644
--- a/src/net/sf/freecol/common/model/Scope.java
+++ b/src/net/sf/freecol/common/model/Scope.java
@@ -198,12 +198,13 @@ public class Scope extends FreeColObject {
if (!type.equals(object.getId())) {
return matchNegated;
}
- } else {
- FreeColSpecObjectType fcgot = object.invokeMethod("getType",
- FreeColSpecObjectType.class, (FreeColSpecObjectType)null);
+ } else if (object instanceof SpecObjectTypeHolder) {
+ FreeColSpecObjectType fcgot = ((SpecObjectTypeHolder)object).getType();
if (fcgot == null || !type.equals(fcgot.getId())) {
return matchNegated;
}
+ } else {
+ return matchNegated;
}
}
if (abilityId != null && object.hasAbility(abilityId) != abilityValue) {
diff --git a/src/net/sf/freecol/common/model/SpecObjectTypeHolder.java b/src/net/sf/freecol/common/model/SpecObjectTypeHolder.java
new file mode 100644
index 00000000000..bcdb30c5204
--- /dev/null
+++ b/src/net/sf/freecol/common/model/SpecObjectTypeHolder.java
@@ -0,0 +1,24 @@
+/**
+ * Copyright (C) 2002-2017 The FreeCol Team
+ *
+ * This file is part of FreeCol.
+ *
+ * FreeCol 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.
+ *
+ * FreeCol 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 FreeCol. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.sf.freecol.common.model;
+
+public interface SpecObjectTypeHolder {
+ public FreeColSpecObjectType getType();
+}
but also defeats (compile time) type safety.
The new interface isn't really required - we could directly check for
BuildingType directly. OTOH, having an interface gives us an bit of
semantic decoupling from Scope.
---
src/net/sf/freecol/common/model/BuildingType.java | 2 +-
src/net/sf/freecol/common/model/Scope.java | 7 ++++---
.../freecol/common/model/SpecObjectTypeHolder.java | 24 ++++++++++++++++++++++
3 files changed, 29 insertions(+), 4 deletions(-)
create mode 100644 src/net/sf/freecol/common/model/SpecObjectTypeHolder.java
diff --git a/src/net/sf/freecol/common/model/BuildingType.java b/src/net/sf/freecol/common/model/BuildingType.java
index 9c6556fbde9..d8ab71471d6 100644
--- a/src/net/sf/freecol/common/model/BuildingType.java
+++ b/src/net/sf/freecol/common/model/BuildingType.java
@@ -40,7 +40,7 @@ import static net.sf.freecol.common.util.CollectionUtils.*;
* of goods it produces and consumes.
*/
public final class BuildingType extends BuildableType
- implements BaseProduction {
+ implements BaseProduction, SpecObjectTypeHolder {
public static final String TAG = "building-type";
diff --git a/src/net/sf/freecol/common/model/Scope.java b/src/net/sf/freecol/common/model/Scope.java
index 7f6f57b1f9f..98c43f0322c 100644
--- a/src/net/sf/freecol/common/model/Scope.java
+++ b/src/net/sf/freecol/common/model/Scope.java
@@ -198,12 +198,13 @@ public class Scope extends FreeColObject {
if (!type.equals(object.getId())) {
return matchNegated;
}
- } else {
- FreeColSpecObjectType fcgot = object.invokeMethod("getType",
- FreeColSpecObjectType.class, (FreeColSpecObjectType)null);
+ } else if (object instanceof SpecObjectTypeHolder) {
+ FreeColSpecObjectType fcgot = ((SpecObjectTypeHolder)object).getType();
if (fcgot == null || !type.equals(fcgot.getId())) {
return matchNegated;
}
+ } else {
+ return matchNegated;
}
}
if (abilityId != null && object.hasAbility(abilityId) != abilityValue) {
diff --git a/src/net/sf/freecol/common/model/SpecObjectTypeHolder.java b/src/net/sf/freecol/common/model/SpecObjectTypeHolder.java
new file mode 100644
index 00000000000..bcdb30c5204
--- /dev/null
+++ b/src/net/sf/freecol/common/model/SpecObjectTypeHolder.java
@@ -0,0 +1,24 @@
+/**
+ * Copyright (C) 2002-2017 The FreeCol Team
+ *
+ * This file is part of FreeCol.
+ *
+ * FreeCol 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.
+ *
+ * FreeCol 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 FreeCol. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.sf.freecol.common.model;
+
+public interface SpecObjectTypeHolder {
+ public FreeColSpecObjectType getType();
+}
--
2.11.0.rc0.7.gbe5a750
2.11.0.rc0.7.gbe5a750