Java Code Examples for org.apache.commons.lang.math.NumberUtils#toFloat()

The following examples show how to use org.apache.commons.lang.math.NumberUtils#toFloat() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ServerController.java    From cachecloud with Apache License 2.0 6 votes vote down vote up
/**
 * parse net to map
 * @param netString
 * @param subnetMap
 * @param isIn
 */
private void addNetMap(String netString, Map<String, NetChart> subnetMap, boolean isIn) {
	String[] subnetArray = netString.split(";");
	for(String subnet : subnetArray) {
		if(StringUtils.isEmpty(subnet)) {
			continue;
		}
		String[] net = subnet.split(",");
		NetChart netChart = subnetMap.get(net[0]);
		if(netChart == null) {
			netChart = new NetChart(net[0]);
			subnetMap.put(net[0], netChart);
		}
		float v = NumberUtils.toFloat(net[1]);
		if(isIn) {
			netChart.addInSeries(v);
			netChart.addTotalIn(v);
			netChart.setMaxIn(v);
		}else {
			netChart.addOutSeries(v);
			netChart.addTotalOut(v);
			netChart.setMaxOut(v);
		}
	}
}
 
Example 2
Source File: ConversionUtils.java    From usergrid with Apache License 2.0 6 votes vote down vote up
public static float getFloat( Object obj ) {
    if ( obj instanceof Float ) {
        return ( Float ) obj;
    }
    if ( obj instanceof Number ) {
        return ( ( Number ) obj ).floatValue();
    }
    if ( obj instanceof String ) {
        return NumberUtils.toFloat( ( String ) obj );
    }
    if ( obj instanceof Date ) {
        return ( ( Date ) obj ).getTime();
    }
    if ( obj instanceof byte[] ) {
        return getFloat( ( byte[] ) obj );
    }
    if ( obj instanceof ByteBuffer ) {
        return getFloat( ( ByteBuffer ) obj );
    }
    return 0;
}
 
Example 3
Source File: ConversionUtils.java    From usergrid with Apache License 2.0 6 votes vote down vote up
public static float getFloat( Object obj ) {
    if ( obj instanceof Float ) {
        return ( Float ) obj;
    }
    if ( obj instanceof Number ) {
        return ( ( Number ) obj ).floatValue();
    }
    if ( obj instanceof String ) {
        return NumberUtils.toFloat( ( String ) obj );
    }
    if ( obj instanceof Date ) {
        return ( ( Date ) obj ).getTime();
    }
    if ( obj instanceof byte[] ) {
        return getFloat( ( byte[] ) obj );
    }
    if ( obj instanceof ByteBuffer ) {
        return getFloat( ( ByteBuffer ) obj );
    }
    return 0;
}
 
Example 4
Source File: ServerController.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
private void addToChart(String line, DiskChart chart) {
	String[] parts = line.split(",");
	for(String part : parts) {
		if(StringUtils.isEmpty(part)) {
			continue;
		}
		String[] values = part.split(":");
		float d = NumberUtils.toFloat(values[1]);
		chart.addSeries(values[0], d);
		chart.setMax(d);
		chart.addTotal(d);
	}
}
 
Example 5
Source File: Load.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
/**
 * line format:
 * BBBP,585,uptime," 09:35:00 up 567 days, 15:07,  0 users,  load average: 0.60, 0.63, 0.67"
 */
public void parse(String line, String timeKey) throws Exception{
	Matcher matcher = PATTERN.matcher(line);
	if(matcher.find()) {
		load1 = NumberUtils.toFloat(matcher.group(1));
		load5 = NumberUtils.toFloat(matcher.group(2));
		load15 = NumberUtils.toFloat(matcher.group(3));
	}
}
 
Example 6
Source File: Memory.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
/**
 * line format:
 * MEM,Memory MB bx-50-13,memtotal,hightotal,lowtotal,swaptotal,memfree,highfree,lowfree,swapfree,memshared,cached,active,bigfree,buffers,swapcached,inactive
 * MEM,T0001,48288.7,0.0,48288.7,8189.4,132.6,0.0,132.6,8189.1,-0.0,24210.6,30819.7,-1.0,153.9,0.0,16451.1
 */
public void parse(String line, String timeKey) throws Exception{
	if(line.startsWith(FLAG)) {
		String[] items = line.split(",");
		if(!items[1].equals(timeKey)) {
			return;
		}
		total = NumberUtils.toFloat(items[2]);
		swap = NumberUtils.toFloat(items[5]);
		totalFree = NumberUtils.toFloat(items[6]);
		swapFree = NumberUtils.toFloat(items[9]);
		cache = NumberUtils.toFloat(items[11]);
		buffer = NumberUtils.toFloat(items[14]);
	}
}
 
Example 7
Source File: MIMEParse.java    From Server.Java with MIT License 5 votes vote down vote up
/**
 * Find the best match for a given mimeType against a list of media_ranges
 * that have already been parsed by MimeParse.parseMediaRange(). Returns a
 * tuple of the fitness value and the value of the 'q' quality parameter of
 * the best match, or (-1, 0) if no match was found. Just as for
 * quality_parsed(), 'parsed_ranges' must be a list of parsed media ranges.
 * 
 * @param mimeType
 * @param parsedRanges
 * @return 
 */
protected static FitnessAndQuality fitnessAndQualityParsed(String mimeType,
        Collection<ParseResults> parsedRanges)
{
    int bestFitness = -1;
    float bestFitQ = 0;
    ParseResults target = parseMediaRange(mimeType);

    for (ParseResults range : parsedRanges)
    {
        if ((target.type.equals(range.type) || range.type.equals("*") || target.type
                .equals("*"))
                && (target.subType.equals(range.subType)
                        || range.subType.equals("*") || target.subType
                        .equals("*")))
        {
            for (String k : target.params.keySet())
            {
                int paramMatches = 0;
                if (!k.equals("q") && range.params.containsKey(k)
                        && target.params.get(k).equals(range.params.get(k)))
                {
                    paramMatches++;
                }
                int fitness = (range.type.equals(target.type)) ? 100 : 0;
                fitness += (range.subType.equals(target.subType)) ? 10 : 0;
                fitness += paramMatches;
                if (fitness > bestFitness)
                {
                    bestFitness = fitness;
                    bestFitQ = NumberUtils
                            .toFloat(range.params.get("q"), 0);
                }
            }
        }
    }
    return new FitnessAndQuality(bestFitness, bestFitQ);
}
 
Example 8
Source File: ParticleDisplay.java    From XSeries with MIT License 4 votes vote down vote up
/**
 * Builds particle settings from a configuration section.
 *
 * @param location the location for this particle settings.
 * @param config   the config section for the settings.
 * @return a parsed ParticleDisplay.
 * @since 1.0.0
 */
@Nonnull
public static ParticleDisplay fromConfig(@Nullable Location location, @Nonnull ConfigurationSection config) {
    Objects.requireNonNull(config, "Cannot parse ParticleDisplay from a null config section");
    Particle particle = XParticle.getParticle(config.getString("particle"));
    if (particle == null) particle = Particle.FLAME;
    int count = config.getInt("count");
    double extra = config.getDouble("extra");
    double offsetx = 0, offsety = 0, offsetz = 0;

    String offset = config.getString("offset");
    if (offset != null) {
        String[] offsets = StringUtils.split(offset, ',');
        if (offsets.length > 0) {
            offsetx = NumberUtils.toDouble(offsets[0]);
            if (offsets.length > 1) {
                offsety = NumberUtils.toDouble(offsets[1]);
                if (offsets.length > 2) {
                    offsetz = NumberUtils.toDouble(offsets[2]);
                }
            }
        }
    }

    double x = 0, y = 0, z = 0;
    String rotation = config.getString("rotation");
    if (rotation != null) {
        String[] rotations = StringUtils.split(rotation, ',');
        if (rotations.length > 0) {
            x = NumberUtils.toDouble(rotations[0]);
            if (rotations.length > 1) {
                y = NumberUtils.toDouble(rotations[1]);
                if (rotations.length > 2) {
                    z = NumberUtils.toDouble(rotations[2]);
                }
            }
        }
    }

    float[] rgbs = null;
    String color = config.getString("color");
    if (color != null) {
        String[] colors = StringUtils.split(rotation, ',');
        if (colors.length >= 3) rgbs = new float[]
                {NumberUtils.toInt(colors[0]), NumberUtils.toInt(colors[1]), NumberUtils.toInt(colors[2]),
                        (colors.length > 3 ? NumberUtils.toFloat(colors[0]) : 1.0f)};
    }

    Vector rotate = new Vector(x, y, z);
    ParticleDisplay display = new ParticleDisplay(particle, location, count, offsetx, offsety, offsetz, extra);
    display.rotation = rotate;
    display.data = rgbs;
    return display;
}
 
Example 9
Source File: NumbersUtil.java    From cosmic with Apache License 2.0 4 votes vote down vote up
public static float parseFloat(final String s, final float defaultValue) {
    return NumberUtils.toFloat(s, defaultValue);
}
 
Example 10
Source File: NumbersUtil.java    From cloudstack with Apache License 2.0 4 votes vote down vote up
public static float parseFloat(String s, float defaultValue) {
    return NumberUtils.toFloat(s, defaultValue);
}
 
Example 11
Source File: MIMEParse.java    From Server.Java with MIT License 3 votes vote down vote up
/**
 * Carves up a media range and returns a ParseResults.
 * 
 * For example, the media range 'application/*;q=0.5' would get parsed into:
 * 
 * ('application', '*', {'q', '0.5'})
 * 
 * In addition this function also guarantees that there is a value for 'q'
 * in the params dictionary, filling it in with a proper default if
 * necessary.
 * 
 * @param range
 * @return 
 */
protected static ParseResults parseMediaRange(String range)
{
    ParseResults results = parseMimeType(range);
    String q = results.params.get("q");
    float f = NumberUtils.toFloat(q, 1);
    if (StringUtils.isBlank(q) || f < 0 || f > 1)
        results.params.put("q", "1");
    return results;
}