Example usage for java.lang Float Float

List of usage examples for java.lang Float Float

Introduction

In this page you can find the example usage for java.lang Float Float.

Prototype

@Deprecated(since = "9")
public Float(String s) throws NumberFormatException 

Source Link

Document

Constructs a newly allocated Float object that represents the floating-point value of type float represented by the string.

Usage

From source file:co.cask.hydrator.plugin.batch.aggreagtor.aggregator.Sampling.java

@Override
public void aggregate(String groupKey, Iterator<StructuredRecord> iterator, Emitter<StructuredRecord> emitter)
        throws Exception {
    int finalSampleSize = 0;
    if (config.sampleSize != null) {
        finalSampleSize = config.sampleSize;
    }//from   w  ww .  ja  va2  s. co m
    if (config.samplePercentage != null) {
        finalSampleSize = Math.round((config.samplePercentage / 100) * config.totalRecords);
    }

    switch (TYPE.valueOf(config.samplingType.toUpperCase())) {
    case SYSTEMATIC:
        if (config.overSamplingPercentage != null) {
            finalSampleSize = Math
                    .round(finalSampleSize + (finalSampleSize * (config.overSamplingPercentage / 100)));
        }

        int sampleIndex = Math.round(config.totalRecords / finalSampleSize);
        Float random = new Float(0);
        if (config.random != null) {
            random = config.random;
        } else {
            random = new Random().nextFloat();
        }
        int firstSampleIndex = Math.round(sampleIndex * random);
        List<StructuredRecord> records = IteratorUtils.toList(iterator);
        int counter = 0;
        emitter.emit(records.get(firstSampleIndex));
        counter++;

        while (counter < finalSampleSize) {
            int index = firstSampleIndex + (counter * sampleIndex);
            emitter.emit(records.get(index - 1));
            counter++;
        }
        break;

    case RESERVOIR:
        PriorityBuffer sampleData = new PriorityBuffer(true, new Comparator<StructuredRecord>() {
            @Override
            public int compare(StructuredRecord o1, StructuredRecord o2) {
                if ((float) o1.get("random") < (float) o2.get("random")) {
                    return 1;
                } else if ((float) o1.get("random") > (float) o2.get("random")) {
                    return -1;
                } else {
                    return 0;
                }
            }
        });

        int count = 0;
        Random randomValue = new Random();
        List<StructuredRecord> recordArray = IteratorUtils.toList(iterator);
        Schema inputSchema = recordArray.get(0).getSchema();
        Schema schemaWithRandomField = createSchemaWithRandomField(inputSchema);
        while (count < finalSampleSize) {
            StructuredRecord record = recordArray.get(0);
            sampleData.add(getSampledRecord(record, randomValue.nextFloat(), schemaWithRandomField));
            count++;
        }

        while (count < recordArray.size()) {
            StructuredRecord structuredRecord = (StructuredRecord) sampleData.get();
            Float randomFloat = randomValue.nextFloat();
            if ((float) structuredRecord.get("random") < randomFloat) {
                sampleData.remove();
                StructuredRecord record = recordArray.get(count);
                sampleData.add(getSampledRecord(record, randomFloat, structuredRecord.getSchema()));
            }
            count++;
        }

        Iterator<StructuredRecord> sampleDataIterator = sampleData.iterator();
        while (sampleDataIterator.hasNext()) {
            StructuredRecord sampledRecord = sampleDataIterator.next();
            StructuredRecord.Builder builder = StructuredRecord.builder(inputSchema);
            for (Schema.Field field : sampledRecord.getSchema().getFields()) {
                if (!field.getName().equalsIgnoreCase("random")) {
                    builder.set(field.getName(), sampledRecord.get(field.getName()));
                }
            }
            emitter.emit(builder.build());
        }
        break;
    }
}

From source file:com.nextdoor.bender.operation.substitution.FormattedSubstitutionTest.java

@Test
public void testStringSubNumberKnown() throws FieldNotFoundException {
    Variable.FieldVariable v = new Variable.FieldVariable();
    v.setFailSrcNotFound(true);/*from  www  . ja  v a  2  s .  co m*/
    v.setSrcFields(Arrays.asList("foo"));

    ArrayList<Substitution> substitutions = new ArrayList<Substitution>();
    substitutions.add(new FormattedSubstitution("bar", new ExtendedMessageFormat("number = {0}"),
            Arrays.asList(v), true));

    DummpyMapEvent devent = new DummpyMapEvent();
    devent.setField("foo", new Float(1.234f));

    InternalEvent ievent = new InternalEvent("", null, 0);
    ievent.setEventObj(devent);

    SubstitutionOperation op = new SubstitutionOperation(substitutions);
    op.perform(ievent);

    assertEquals(2, devent.payload.size());
    assertEquals("number = 1.234", devent.getField("bar"));
}

From source file:nz.co.senanque.validationengine.ConvertUtils.java

public static Object convertToObject(Class<?> clazz) {
    if (clazz.isPrimitive()) {
        if (clazz.equals(Long.TYPE)) {
            return new Long(0L);
        } else if (clazz.equals(Integer.TYPE)) {
            return new Integer(0);
        } else if (clazz.equals(Float.TYPE)) {
            return new Float(0F);
        } else if (clazz.equals(Double.TYPE)) {
            return new Double(0D);
        } else if (clazz.equals(Boolean.TYPE)) {
            return new Boolean(false);
        }//  ww w .  j a v  a 2  s .  c o m
    }
    return null;

}

From source file:com.neovisionaries.security.DigestTest.java

@Test
public void test2() {
    List<Number> list = new ArrayList<Number>();
    list.add(new Byte((byte) 1));
    list.add(new Short((short) 2));
    list.add(new Integer(3));
    list.add(new Long(4));
    list.add(new Float(5));
    list.add(new Double(6));

    String digest1 = sha1().update(list).digestAsString();

    String digest2 = sha1().update((byte) 1).update((short) 2).update(3).update((long) 4).update((float) 5)
            .update((double) 6).digestAsString();

    assertEquals(digest1, digest2);/*from   ww  w  .ja  v a2 s  . com*/
}

From source file:com.tesora.dve.common.TestDataGenerator.java

protected Object getColumnValue(ColumnMetadata cm) {
    Object cv = null;// ww  w  . ja  v  a2s .  c  o m
    Calendar cal = Calendar.getInstance();

    switch (cm.getDataType()) {
    case Types.BIT:
    case Types.BOOLEAN:
        cv = Boolean.TRUE;
        break;
    case Types.BIGINT:
        cv = Long.MAX_VALUE;
        break;
    case Types.CHAR:
    case Types.VARCHAR:
        cv = StringUtils.left(baseString, cm.getSize());
        break;
    case Types.SMALLINT:
        cv = Short.MAX_VALUE;
        break;
    case Types.TINYINT:
        cv = Byte.MAX_VALUE;
        break;
    case Types.INTEGER:
        cv = Integer.MAX_VALUE;
        break;
    case Types.DOUBLE:
        cv = new Double(1234.5678); // TODO need to handle s,p
        break;
    case Types.FLOAT:
        cv = new Float(123.56); // TODO need to handle s,p
        break;
    case Types.DECIMAL:
        cv = new BigDecimal("12345.6789"); // TODO need to handle s,p
        break;
    case Types.DATE:
        cal.setTimeInMillis(123456789);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        cv = cal.getTime();
        break;
    case Types.TIMESTAMP:
        cal.setTimeInMillis(123456789);
        cv = cal.getTime();
        break;
    case Types.TIME:
        cv = new Time(123456789);
        break;
    default:
        break;
    }

    return cv;
}

From source file:com.jaeksoft.searchlib.script.CommandAbstract.java

protected Float getParameterFloat(int pos) {
    String s = getParameterString(pos);
    if (StringUtils.isEmpty(s))
        return null;
    return new Float(s);
}

From source file:com.mongodb.hadoop.mapred.input.BSONFileRecordReader.java

public float getProgress() throws IOException {
    if (this.finished)
        return 1f;
    if (in != null)
        return new Float(in.getPos() - this.fileSplit.getStart()) / this.fileSplit.getLength();
    return 0f;/*from   ww  w .jav a 2 s . com*/
}

From source file:Main.java

static Object convert(Object[] objects, Object var) {
    Object newVar = getObject(objects);

    if (newVar instanceof Number) {
        Number newNum = (Number) var;

        if (newVar instanceof Integer) {
            return new Integer(newNum.intValue());
        } else if (newVar instanceof Long) {
            return new Long(newNum.longValue());
        } else if (newVar instanceof Float) {
            return new Float(newNum.floatValue());
        } else if (newVar instanceof Double) {
            return new Double(newNum.doubleValue());
        } else {/*from   w  w w  . j  a v a2s . com*/
            return null;
        }
    } else if (newVar instanceof String) {
        return new String((String) newVar);
    } else {
        //TODO: add other classes
        return null;
    }
}

From source file:com.robonobo.common.util.BeanPropertyAccessor.java

public void setProperty(String name, float value) throws Exception {
    setProperty(name, float.class, new Float(value));
}

From source file:me.willowcheng.makerthings.model.OpenHABItem.java

public Float getStateAsFloat() {
    // For uninitialized/null state return zero
    if (state == null) {
        return new Float(0);
    }//ww  w  .  j a  va 2 s .  c  om
    if ("ON".equals(state)) {
        return new Float(100);
    }
    if ("OFF".equals(state)) {
        return new Float(0);
    }
    try {
        Float result = Float.parseFloat(state);
    } catch (NumberFormatException e) {
        if (e != null) {
            Crittercism.logHandledException(e);
            Log.e(TAG, e.getMessage());
        }

    }
    return Float.parseFloat(state);
}