0

我将 EnumMap 提供给我尝试访问该值的方法,如下所示:

_playerActionTimeout = (Integer) config.get( Config.Table.PLAYER_TIMEOUT );

但这告诉我我不能将 Config.Table 转换为 Config.Table。配置位如下所示:

static public enum Table
{
    PLAYER_TIMEOUT( "playerTimeout", Integer.class ),
    PLAYER_STARTCHIPS( "playerStartChips", Integer.class ),
    INITIAL_SMALLBLIND( "initialSmallBlind", Integer.class ),
    INITIAL_BIGBLIND( "initialBigBlind", Integer.class ),
    BLIND_INCREASE( "blindIncrease", SortedMap.class ),
    ROUND_STARTTIMEOUT( "roundStartTimeout", Integer.class );
    //
    private final String _xmlTag;
    private final Class _classType;

    Table( String xmlTag, Class classType )
    {
        _xmlTag = xmlTag;
        _classType = classType;
    }

    public final String getXmlTag()
    {
        return _xmlTag;
    }

    public final Class getParameterClass()
    {
        return _classType;
    }
}

如果我遍历 EnumMap 所有值和键都存在并且可以很好地追踪。但是当我尝试使用 Enum 获取值时,它将为零并且发生错误。我究竟做错了什么?

按照要求。这是创建配置的类的一部分:

private EnumMap<Config.Table, Object> _tableConfig;

public void init()
{
if ( _tableConfig == null )
    {
        _tableConfig = new EnumMap<Config.Table, Object>( Config.Table.class );
    }
}
private void parseConfigFile( String definedZoneName ) throws Exception
{
    //try
    {
        /** START of read config file **/
        IXMLElement config = this.loadConfig( "config.xml" );
        if ( config == null )
        {
            throw new Exception( "No config file was found! or XML zones configuration invalid : no zone found" );
        }
        IXMLElement zones = config.getFirstChildNamed( "Zones" );
        //So far so good
        for ( int zoneIndex = 0; zoneIndex < zones.getChildrenCount(); ++zoneIndex )
        {
            IXMLElement xmlZone = zones.getChildAtIndex( zoneIndex );
            if ( xmlZone.getAttribute( "name" ).equalsIgnoreCase( definedZoneName ) )
            {
            //Table config
            {
                IXMLElement tableConfig = xmlZone.getFirstChildNamed( "TableConfig" );
                for ( int tableElIndex = 0; tableElIndex < tableConfig.getChildrenCount(); ++tableElIndex )
                {
                    IXMLElement zoneElement = tableConfig.getChildAtIndex( tableElIndex );

                    for ( Config.Table configTable : Config.Table.values() )
                    {
                        if ( zoneElement.getName().equals( configTable.getXmlTag() ) )
                        {
                            String value = zoneElement.getContent();
                            Class parameterClass = configTable.getParameterClass();
                            if ( parameterClass.equals( Integer.class ) )
                            {
                                _tableConfig.put( configTable, Integer.parseInt( value ) );
                            }
                            else if ( parameterClass.equals( SortedMap.class ) )
                            {
                                SortedMap<Integer, Config.Blinds> increaseBlinds = new TreeMap<Integer, Config.Blinds>();
                                //Loop through all <increase> elements
                                for ( int increaseElIndex = 0; increaseElIndex < zoneElement.getChildrenCount(); ++increaseElIndex )
                                {
                                    IXMLElement increaseElement = zoneElement.getChildAtIndex( increaseElIndex );

                                    String sTime = increaseElement.getFirstChildNamed( "time" ).getContent();
                                    String sSmallBlind = increaseElement.getFirstChildNamed( "smallBlind" ).getContent();
                                    String sBigBlind = increaseElement.getFirstChildNamed( "bigBlind" ).getContent();

                                    Integer time = Integer.parseInt( sTime );
                                    Integer smallBlind = Integer.parseInt( sSmallBlind );
                                    Integer bigBlind = Integer.parseInt( sBigBlind );

                                    Config.Blinds blinds = new Config.Blinds( smallBlind, bigBlind );

                                    increaseBlinds.put( time, blinds );
                                }

                                _tableConfig.put( configTable, increaseBlinds );
                            }
                            else //if ( parameterClass.equals( String.class ) )
                            {
                                _tableConfig.put( configTable, value );
                            }
                            break;
                        }
                    }
                }
            }
        }
    }
    Stack<String> missingFields = new Stack<String>();

    for ( Config.Zone configZone : Config.Zone.values() )
    {
        if ( !_zoneConfig.containsKey( configZone ) )
        {
            missingFields.add( configZone.getXmlTag() );
        }
    }

    if ( missingFields.size() > 0 )
    {
        throw new Exception( "We were not able to read these Zone settings from the config.xml : " + missingFields.toString() + ". Please check your setup." );
    }

    missingFields.clear();
    for ( Config.Table configTable : Config.Table.values() )
    {
        if ( !_tableConfig.containsKey( configTable ) )
        {
            missingFields.add( configTable.getXmlTag() );
        }
    }

    if ( missingFields.size() > 0 )
    {
        throw new Exception( "We were not able to read these Table settings from the config.xml : " + missingFields.toString() + ". Please check your setup." );
    }
}
}
4

0 回答 0