Example usage for com.google.gson FieldAttributes getName

List of usage examples for com.google.gson FieldAttributes getName

Introduction

In this page you can find the example usage for com.google.gson FieldAttributes getName.

Prototype

public String getName() 

Source Link

Usage

From source file:helper.JsonExcludeStrategy.java

@Override
public boolean shouldSkipField(FieldAttributes f) {

    if (f.getName().equalsIgnoreCase("employeeId"))
        return false;

    return true;/*from w ww .ja va 2s.co m*/
}

From source file:info.raack.appliancedetection.evaluation.web.EvaluationController.java

License:Open Source License

@RequestMapping(value = "/evaluation/{id}", method = RequestMethod.GET)
public void getEvaluationData(@PathVariable("id") String simulationId,
        @RequestParam("algorithmId") int algorithmId,
        @RequestParam(value = "start", required = false) Double startMillis,
        @RequestParam(value = "end", required = false) Double endMillis,
        @RequestParam(value = "ticks", required = false) Integer ticks, HttpServletRequest request,
        HttpServletResponse response) throws IOException {

    // TODO - for now, just use the naive algorithm's energy measurements.

    Date start = null;/*from w  w w  .j a  v  a2 s  . com*/
    Date end = null;

    if (startMillis != null && endMillis != null) {
        start = new Date(startMillis.longValue());
        end = new Date(endMillis.longValue());
    } else if (startMillis != null && endMillis == null) {
        // if only start or end are provided, create a one day span
        Calendar c = new GregorianCalendar();
        c.setTimeInMillis(startMillis.longValue());
        start = c.getTime();
    } else if (startMillis == null && endMillis != null) {
        // if only start or end are provided, create a one day span
        Calendar c = new GregorianCalendar();
        c.setTimeInMillis(endMillis.longValue());
        end = c.getTime();

    }

    if (ticks == null) {
        ticks = 300;
    }

    Evaluation evaluation = simulationService.getEvaluation(algorithmId, simulationId, start, end, true);

    if (evaluation == null) {
        response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        return;
    }

    EvaluationWrapper wrapper = new EvaluationWrapper(evaluation);

    JsonSerializer<EnergyTimestep> dateSerializer = new JsonSerializer<EnergyTimestep>() {
        // serialize date to milliseconds since epoch
        public JsonElement serialize(EnergyTimestep energyTimestep, Type me, JsonSerializationContext arg2) {
            JsonArray object = new JsonArray();
            object.add(new JsonPrimitive(energyTimestep.getStartTime().getTime()));
            object.add(new JsonPrimitive(energyTimestep.getEnergyConsumed()));
            return object;
        }
    };

    String dataJS = new GsonBuilder().registerTypeAdapter(EnergyTimestep.class, dateSerializer)
            .excludeFieldsWithModifiers(Modifier.STATIC).setExclusionStrategies(new ExclusionStrategy() {

                // skip logger
                public boolean shouldSkipClass(Class<?> clazz) {
                    return clazz.equals(Logger.class);
                }

                public boolean shouldSkipField(FieldAttributes fieldAttributes) {
                    // skip simulation of simulated appliance
                    return (fieldAttributes.getName().equals("simulation")
                            && fieldAttributes.getDeclaringClass() == SimulatedAppliance.class) ||
                    // skip simulation second data
                    (fieldAttributes.getName().equals("secondData")
                            && fieldAttributes.getDeclaringClass() == Simulation.class) ||
                    // skip endTime of energytimestep
                    (fieldAttributes.getName().equals("endTime")
                            && fieldAttributes.getDeclaringClass() == EnergyTimestep.class) ||
                    // skip userAppliance, detectionAlgorithmId of appliance state transition
                    ((fieldAttributes.getName().equals("userAppliance")
                            || fieldAttributes.getName().equals("detectionAlgorithmId"))
                            && fieldAttributes.getDeclaringClass() == ApplianceStateTransition.class);
                }
            }).create().toJson(wrapper);

    response.getWriter().write(dataJS);
    response.setContentType("application/json");
}

From source file:info.raack.appliancedetection.evaluation.web.EvaluationController.java

License:Open Source License

@RequestMapping(value = "/evaluationgroup/{id}", method = RequestMethod.GET)
public void getEvaluationGroupData(@PathVariable("id") int simulationGroupId, HttpServletRequest request,
        HttpServletResponse response) throws IOException {

    EvaluationGroup evaluationGroup = simulationService.getEvaluationGroup(simulationGroupId);

    if (evaluationGroup == null) {
        response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        return;/*from  www  .j  a  v  a  2 s.co m*/
    }

    String dataJS = new GsonBuilder().excludeFieldsWithModifiers(Modifier.STATIC)
            .setExclusionStrategies(new ExclusionStrategy() {

                // skip logger
                public boolean shouldSkipClass(Class<?> clazz) {
                    return clazz.equals(Logger.class);
                }

                public boolean shouldSkipField(FieldAttributes fieldAttributes) {
                    // skip everything in evaluation except overallEnergyError or simulation
                    return (!(fieldAttributes.getName().equals("stateTransitionPrecision")
                            || fieldAttributes.getName().equals("stateTransitionRecall")
                            || fieldAttributes.getName().equals("stateTransitionAccuracy")
                            || fieldAttributes.getName().equals("overallAccuracy")
                            || fieldAttributes.getName().equals("overallEnergyError")
                            || fieldAttributes.getName().equals("simulation"))
                            && fieldAttributes.getDeclaringClass() == Evaluation.class) ||
                    // skip everything in simulation except id and durationInSeconds
                    (!(fieldAttributes.getName().equals("id")
                            || fieldAttributes.getName().equals("durationInSeconds"))
                            && fieldAttributes.getDeclaringClass() == Simulation.class);
                }
            }).create().toJson(evaluationGroup);

    response.getWriter().write(dataJS);
    response.setContentType("application/json");
}

From source file:info.raack.appliancelabeler.web.MainController.java

License:Open Source License

@RequestMapping(value = "/energy/{frequency}", method = RequestMethod.GET)
public void getEnergyData(@PathVariable("frequency") String frequency,
        @RequestParam(value = "start", required = false) Double startMillis,
        @RequestParam(value = "end", required = false) Double endMillis,
        @RequestParam(value = "ticks", required = false) Integer ticks, HttpServletRequest request,
        HttpServletResponse response) throws IOException {
    // need to get latest values from stepgreen service

    EnergyMonitor energyMonitor = getCurrentEnergyMonitor(request, response);

    Date start = new Date();
    Date end = new Date();

    if (startMillis != null && endMillis != null) {
        start = new Date(startMillis.longValue());
        end = new Date(endMillis.longValue());
    } else if (startMillis != null && endMillis == null) {
        // if only start or end are provided, create a one day span
        Calendar c = new GregorianCalendar();
        c.setTimeInMillis(startMillis.longValue());
        start = new Date();
        start.setTime(startMillis.longValue());

        c.add(Calendar.DATE, 1);// w  w  w.  j a va  2 s .c om
        end = c.getTime();
    } else if (startMillis == null && endMillis != null) {
        // if only start or end are provided, create a one day span
        Calendar c = new GregorianCalendar();
        c.setTimeInMillis(endMillis.longValue());
        end = new Date();
        end.setTime(endMillis.longValue());

        c.add(Calendar.DATE, -1);
        start = c.getTime();

    } else {
        createOneDaySpan(energyMonitor, start, end);
    }

    if (ticks == null) {
        ticks = 300;
    }

    Date queryStart = null;
    Date queryEnd = null;

    // if the time window is less than 5 minutes, then just take the window as is; otherwise, enlarge the window to the 5 minute interval requested
    if (end.getTime() - start.getTime() > (5 * 60 * 1000)) {
        Calendar cal = new GregorianCalendar();
        cal.setTime(start);
        queryStart = dateUtils.getPreviousFiveMinuteIncrement(cal).getTime();

        cal = new GregorianCalendar();
        cal.setTime(end);
        queryEnd = dateUtils.getNextFiveMinuteIncrement(cal).getTime();
    } else {
        queryStart = start;
        queryEnd = end;
    }

    List<SecondData> data = getEnergyDataWithLimits(energyMonitor,
            DataService.DataFrequency.valueOf(frequency.toUpperCase()), queryStart, queryEnd, ticks);
    Map<UserAppliance, List<EnergyTimestep>> predictedEnergyUsage = dataService
            .getApplianceEnergyConsumptionForMonitor(energyMonitor, algorithm.getId(), queryStart, queryEnd);
    Map<UserAppliance, List<ApplianceStateTransition>> predictedApplianceStateTransitions = dataService
            .getPredictedApplianceStateTransitionsForMonitor(energyMonitor, algorithm.getId(), queryStart,
                    queryEnd);

    JsonSerializer<EnergyTimestep> dateSerializer = new JsonSerializer<EnergyTimestep>() {
        // serialize date to milliseconds since epoch
        public JsonElement serialize(EnergyTimestep energyTimestep, Type me, JsonSerializationContext arg2) {
            JsonArray object = new JsonArray();
            object.add(new JsonPrimitive(energyTimestep.getStartTime().getTime()));
            object.add(new JsonPrimitive(energyTimestep.getEnergyConsumed()));
            return object;
        }
    };

    EnergyWrapper energyWrapper = new EnergyWrapper(data, predictedEnergyUsage,
            predictedApplianceStateTransitions, database.getEnergyCost(energyMonitor));

    String dataJS = new GsonBuilder().registerTypeAdapter(EnergyTimestep.class, dateSerializer)
            .excludeFieldsWithModifiers(Modifier.STATIC).setExclusionStrategies(new ExclusionStrategy() {

                // skip logger
                public boolean shouldSkipClass(Class<?> clazz) {
                    return clazz.equals(Logger.class);
                }

                public boolean shouldSkipField(FieldAttributes fieldAttributes) {
                    // skip endTime of energytimestep
                    return (fieldAttributes.getName().equals("endTime")
                            && fieldAttributes.getDeclaringClass() == EnergyTimestep.class) ||
                    // skip userAppliance, detectionAlgorithmId of appliance state transition
                    ((fieldAttributes.getName().equals("userAppliance")
                            || fieldAttributes.getName().equals("detectionAlgorithmId"))
                            && fieldAttributes.getDeclaringClass() == ApplianceStateTransition.class);
                }
            }).create().toJson(energyWrapper);

    response.getWriter().write(dataJS);

    // set appropriate JSON response type
    response.setContentType("application/json");
}

From source file:io.barracks.ota.client.Utils.java

License:Apache License

public static Gson getRobolectricGson(GsonBuilder builder) {
    return builder.setExclusionStrategies(new ExclusionStrategy() {
        @Override//from   w  w  w .  j  ava  2 s. c  o m
        public boolean shouldSkipField(FieldAttributes f) {
            return "__robo_data__".equals(f.getName());
        }

        @Override
        public boolean shouldSkipClass(Class<?> clazz) {
            return false;
        }
    }).create();
}

From source file:ispyb.server.biosaxs.vos.utils.serializer.ExperimentExclusionStrategy.java

License:Open Source License

public boolean shouldSkipField(FieldAttributes f) {
    if (this.experimentScope.equals(ExperimentScope.MINIMAL)) {
        if (this.MINIMAL_EXCLUDE.contains(f.getName())) {
            return true;
        }/*from  w w w.ja  v a  2s  .c  o m*/
    }

    if (this.experimentScope.equals(ExperimentScope.MEDIUM)) {
        if (this.MEDIUM_EXCLUDE.contains(f.getName())) {
            return true;
        }
    }

    if (this.experimentScope.equals(ExperimentScope.MEDIUM)) {
        if (this.FULL_EXCLUDE.contains(f.getName())) {
            return true;
        }
    }

    return false;
}

From source file:ispyb.server.biosaxs.vos.utils.serializer.ProposalExclusionStrategy.java

License:Open Source License

@Override
public boolean shouldSkipField(FieldAttributes f) {
    return f.getName() == "sampleplateposition3VOs";
}

From source file:ispyb.server.biosaxs.vos.utils.serializer.SpecimenExclusionStrategy.java

License:Open Source License

public boolean shouldSkipField(FieldAttributes f) {
    if ((f.getName().equals("merge3VOs"))) {
        return true;
    }//from   ww  w  .  java 2  s.  c o m
    return false;
}

From source file:logic.DiskIOManager.java

License:Open Source License

public static void saveLibrary(MangaLibrary library, SavePart part) {
    try {//from w ww.  ja v  a2  s .c  om
        String configDirectory = library.getConfigDirectory();
        File configDir = new File(configDirectory);
        if (!Files.exists(Paths.get(configDir.toURI())))
            configDir.mkdirs();

        assert library != null;
        Gson gson = new GsonBuilder().setPrettyPrinting().setExclusionStrategies(new ExclusionStrategy() {
            public boolean shouldSkipClass(Class<?> clazz) {
                return false;
            }

            public boolean shouldSkipField(FieldAttributes f) {
                return (f.getName().equals("configDirectory")) || (f.getName().equals("available"));
            }
        }).create();

        String generatedLibrary = gson.toJson(library, library.getClass());
        String generatedAvailable = gson.toJson(library.getAvailable(), library.getAvailable().getClass());

        if (part == SavePart.COLLECTIONS)
            saveLibraryPart(generatedLibrary, configDirectory, "library.json");
        if (part == SavePart.AVAILABLE)
            saveLibraryPart(generatedAvailable, configDirectory, "available.json");

    } catch (IOException e) {
        e.printStackTrace();
        M.print(e.getMessage());
    }
}

From source file:managedbeans.MyExclusionStrategy.java

public boolean shouldSkipField(FieldAttributes f) {
    return (f.getDeclaringClass() == paciente.class && f.getName().equals("CLAPS"))
            || (f.getDeclaringClass() == clap.class && f.getName().equals("paciente"))
            || (f.getDeclaringClass() == region.class && f.getName().equals("comunas"))
            || (f.getDeclaringClass() == comuna.class && f.getName().equals("region"))
            || (f.getDeclaringClass() == cesfam.class && f.getName().equals("comuna"))
            || (f.getDeclaringClass() == Crafft.class && f.getName().equals("clap"))
            || (f.getDeclaringClass() == audit.class && f.getName().equals("clap")) || f.getName().equals("row")
            || f.getName().startsWith("_");

}