From 127582ca1c82325b5cbfd52dd6c6d3503c30c729 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Wed, 27 Mar 2002 21:48:41 +0000 Subject: [PATCH] Be more robust about selecting random components. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@1158 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- .../com/threerings/cast/util/CastUtil.java | 25 +++++++++++++++---- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/java/com/threerings/cast/util/CastUtil.java b/src/java/com/threerings/cast/util/CastUtil.java index bbef4cc9c..b92352c6b 100644 --- a/src/java/com/threerings/cast/util/CastUtil.java +++ b/src/java/com/threerings/cast/util/CastUtil.java @@ -1,5 +1,5 @@ // -// $Id: CastUtil.java,v 1.7 2002/03/27 20:31:11 mdb Exp $ +// $Id: CastUtil.java,v 1.8 2002/03/27 21:48:41 mdb Exp $ package com.threerings.cast.util; @@ -33,12 +33,23 @@ public class CastUtil for (int i = 0; i < CLASSES.length; i++) { String cname = gender + "/" + CLASSES[i]; ComponentClass cclass = crepo.getComponentClass(cname); + + // make sure the component class exists if (cclass == null) { Log.warning("Missing definition for component class " + "[class=" + cname + "]."); - } else { - classes.add(cclass); + continue; } + + // make sure there are some components in this class + Iterator iter = crepo.enumerateComponentIds(cclass); + if (!iter.hasNext()) { + Log.info("Skipping class for which we have no components " + + "[class=" + cclass + "]."); + continue; + } + + classes.add(cclass); } // select the components @@ -53,8 +64,12 @@ public class CastUtil CollectionUtil.addAll(choices, iter); // choose a random component - int idx = RandomUtil.getInt(choices.size()); - components[ii] = ((Integer)choices.get(idx)).intValue(); + if (choices.size() > 0) { + int idx = RandomUtil.getInt(choices.size()); + components[ii] = ((Integer)choices.get(idx)).intValue(); + } else { + Log.info("Have no components in class [class=" + cclass + "]."); + } } return new CharacterDescriptor(components, null);