Example usage for java.lang Long parseLong

List of usage examples for java.lang Long parseLong

Introduction

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

Prototype

public static long parseLong(String s) throws NumberFormatException 

Source Link

Document

Parses the string argument as a signed decimal long .

Usage

From source file:com.wibidata.shopping.model.Category.java

public static Category fromProducts(Node node) throws MalformedURLException {
    Category category = new Category();
    category.setName(StringUtils.removeStart(node.getLabel().toString(), "category:"));
    for (Edge edge : node.getEdges()) {
        Product product = new Product();
        product.setId(edge.getTarget().getLabel().toString());
        AvroMapReader<String> annotations = AvroMapReader
                .<String>create((Map<CharSequence, String>) (Map<? extends CharSequence, String>) edge
                        .getTarget().getAnnotations());
        product.setName(annotations.get("name").toString());
        product.setDescription(annotations.get("description_short").toString());
        product.setThumbnail(annotations.get("thumbnail").toString());
        product.setInventory(Long.parseLong(annotations.get("inventory").toString()));
        product.setPrice(Double.parseDouble(annotations.get("price").toString()));
        category.addProduct(product);/*ww  w .j  ava2s. co m*/
    }
    return category;
}

From source file:com.pytsoft.cachelock.util.KeyUtils.java

/**
 * Parse lock expiration time from lock value stored in lock key.
 *
 * <p>If parameter is null or with invalid format, then return 0.
 *
 * @param lockValue//from   w  ww.  java  2 s  .c o m
 *         Lock value stored in lock key
 *
 * @return Lock expiration time in milliseconds
 */
public static long parseTime(String lockValue) {
    if (lockValue != null) {
        try {
            String timeStr = StringUtils.split(lockValue, Constants.NAMESPACE_SEPARATOR)[1];
            return Long.parseLong(timeStr);
        } catch (Exception e) {
            LOG.warn(String.format("Input lock value[%s] parsed failed, reason:[%s], will return 0.", lockValue,
                    e.getMessage()));
            return 0;
        }
    }
    return 0;
}

From source file:com.fatsecret.platform.utils.RecipeUtility.java

/**
 * Returns detailed information about the recipe
 * //  ww w  .ja  v a2 s . com
 * @param json         json object representing of the recipe
 * @return            detailed information about the recipe
 */
public static Recipe parseRecipeFromJSONObject(JSONObject json) {
    String name = json.getString("recipe_name");
    String url = json.getString("recipe_url");
    String description = json.getString("recipe_description");
    Long id = Long.parseLong(json.getString("recipe_id"));

    List<String> images = new ArrayList<String>();
    JSONObject recipeImages = json.getJSONObject("recipe_images");
    JSONArray recipeImage = null;
    try {
        recipeImage = recipeImages.getJSONArray("recipe_image");
    } catch (Exception e) {
        recipeImage = null;
    }

    if (recipeImage != null) {
        for (int i = 0; i < recipeImage.length(); i++) {
            String image = recipeImage.getString(i);
            images.add(image);
        }
    } else {
        String image = recipeImages.getString("recipe_image");
        images.add(image);
    }

    Integer rating = Integer.parseInt(json.getString("rating"));

    List<String> types = new ArrayList<String>();
    JSONObject recipeTypes = json.getJSONObject("recipe_types");

    JSONArray recipeType = null;
    try {
        recipeType = recipeTypes.getJSONArray("recipe_type");
    } catch (Exception e) {
        recipeType = null;
    }

    if (recipeType != null) {
        for (int i = 0; i < recipeType.length(); i++) {
            String type = recipeType.getString(i);
            types.add(type);
        }
    } else {
        String type = recipeTypes.getString("recipe_type");
        types.add(type);
    }

    BigDecimal numberOfServings = new BigDecimal(json.getString("number_of_servings"));

    Integer preparationTime = 0;
    try {
        preparationTime = Integer.parseInt(json.getString("preparation_time_min"));
    } catch (Exception ignore) {
        preparationTime = 0;
    }

    Integer cookingTime = 0;

    try {
        cookingTime = Integer.parseInt(json.getString("cooking_time_min"));
    } catch (Exception ignore) {
        cookingTime = 0;
    }

    List<Category> categories = new ArrayList<Category>();
    JSONObject recipeCategories = json.getJSONObject("recipe_categories");
    JSONArray recipeCategory = null;

    try {
        recipeCategory = recipeCategories.getJSONArray("recipe_category");
    } catch (Exception e) {
        recipeCategory = null;
    }

    if (recipeCategory != null) {
        for (int i = 0; i < recipeCategory.length(); i++) {
            JSONObject recipeCategoryObj = recipeCategory.getJSONObject(i);
            Category category = parseJsonToCategory(recipeCategoryObj);
            categories.add(category);
        }
    } else {
        JSONObject recipeCategoryObj = recipeCategories.getJSONObject("recipe_category");
        Category category = parseJsonToCategory(recipeCategoryObj);
        categories.add(category);
    }

    JSONObject servingSizes = json.getJSONObject("serving_sizes");
    JSONObject servingObj = servingSizes.getJSONObject("serving");
    Serving serving = ServingUtility.parseServingFromJSONObject(servingObj);

    JSONObject directionsObj = json.getJSONObject("directions");
    List<Direction> directions = new ArrayList<Direction>();
    JSONArray directionArray = null;
    try {
        directionArray = directionsObj.getJSONArray("direction");
    } catch (Exception e) {
        directionArray = null;
    }

    if (directionArray != null) {
        for (int i = 0; i < directionArray.length(); i++) {
            JSONObject directionObj = directionArray.getJSONObject(i);
            Direction direction = parseJsonToDirection(directionObj);
            directions.add(direction);
        }
    } else {
        JSONObject directionObj = directionsObj.getJSONObject("direction");
        Direction direction = parseJsonToDirection(directionObj);
        directions.add(direction);
    }

    List<Ingredient> ingredients = new ArrayList<Ingredient>();
    JSONObject ingredientsObj = json.getJSONObject("ingredients");
    JSONArray ingredientArray = null;
    try {
        ingredientArray = ingredientsObj.getJSONArray("ingredient");
    } catch (Exception e) {
        ingredientArray = null;
    }

    if (ingredientArray != null) {
        for (int i = 0; i < ingredientArray.length(); i++) {
            JSONObject ingredientObj = ingredientArray.getJSONObject(i);
            Ingredient ingredient = parseJsonToIngredient(ingredientObj);
            ingredients.add(ingredient);
        }
    } else {
        JSONObject ingredientObj = ingredientsObj.getJSONObject("ingredient");
        Ingredient ingredient = parseJsonToIngredient(ingredientObj);
        ingredients.add(ingredient);
    }

    Recipe recipe = new Recipe();

    recipe.setName(name);
    recipe.setUrl(url);
    recipe.setDescription(description);
    recipe.setId(id);
    recipe.setImages(images);
    recipe.setRating(rating);
    recipe.setTypes(types);
    recipe.setNumberOfServings(numberOfServings);
    recipe.setPreparationTime(preparationTime);
    recipe.setCookingTime(cookingTime);
    recipe.setCategories(categories);
    recipe.setServing(serving);
    recipe.setDirections(directions);
    recipe.setIngredients(ingredients);

    return recipe;
}

From source file:com.yahoo.elide.utils.coerce.converters.EpochToDateConverter.java

private static <T> T stringToDate(Class<T> cls, String epoch) throws ReflectiveOperationException {
    return longToDate(cls, Long.parseLong(epoch));
}

From source file:cz.muni.fi.pa165.creatures.rest.client.utils.CommandLineValidator.java

/**
 * Validate a command line./*  w w w  .  ja v  a 2 s. com*/
 *
 * @param line command line to validate
 * @return true if command line is valid, false otherwise
 */
public static boolean validate(CommandLine line) {
    if ((!line.hasOption("r") && !line.hasOption("w")) || (line.hasOption("r") && line.hasOption("w"))) {
        return false;
    }

    // WEAPON
    if (line.hasOption("w")) {
        if (!line.hasOption("o")) {
            return false;
        }
        String operation = line.getOptionValue("o");
        if (operation.equals("C")) {
            if (!line.hasOption("n")) {
                return false;
            }
            if (line.hasOption("i")) {
                return false;
            }
            if (line.hasOption("m")) {
                try {
                    Long.parseLong(line.getOptionValue("m"));
                } catch (NumberFormatException ex) {
                    return false;
                }
            }
            if (line.hasOption("g")) {
                try {
                    Long.parseLong(line.getOptionValue("g"));
                } catch (NumberFormatException ex) {
                    return false;
                }
            }
        } else if (operation.equals("R")) {
            if (!line.hasOption("i")) {
                return false;
            } else {
                try {
                    Long.parseLong(line.getOptionValue("i"));
                } catch (NumberFormatException ex) {
                    return false;
                }
            }
        } else if (operation.equals("U")) {
            if (!line.hasOption("i")) {
                return false;
            } else {
                try {
                    Long.parseLong(line.getOptionValue("i"));
                } catch (NumberFormatException ex) {
                    return false;
                }
            }
            if (line.hasOption("m")) {
                try {
                    Long.parseLong(line.getOptionValue("m"));
                } catch (NumberFormatException ex) {
                    return false;
                }
            }
            if (line.hasOption("g")) {
                try {
                    Long.parseLong(line.getOptionValue("g"));
                } catch (NumberFormatException ex) {
                    return false;
                }
            }
        } else if (operation.equals("D")) {
            if (!line.hasOption("i")) {
                return false;
            } else {
                try {
                    Long.parseLong(line.getOptionValue("i"));
                } catch (NumberFormatException ex) {
                    return false;
                }
            }
        } else if (operation.equals("A")) {
            // All records query
        } else if (operation.equals("N")) {
            // Number of records query
        } else {
            // nothing like C, R, U, D, A or N
            return false;
        }
    }

    // REGION
    if (line.hasOption("r")) {
        if (!line.hasOption("o")) {
            return false;
        }
        String operation = line.getOptionValue("o");
        if (operation.equals("C")) {
            if (!line.hasOption("n")) {
                return false;
            }
            if (line.hasOption("i")) {
                return false;
            }
            if (line.hasOption("d")) {
                if (line.getOptionValue("d").length() > 512) {
                    return false;
                }
            }
            if (line.hasOption("a")) {
                try {
                    Long.parseLong(line.getOptionValue("a"));
                } catch (NumberFormatException ex) {
                    return false;
                }
            }
        } else if (operation.equals("R")) {
            if (!line.hasOption("i")) {
                return false;
            } else {
                try {
                    Long.parseLong(line.getOptionValue("i"));
                } catch (NumberFormatException ex) {
                    return false;
                }
            }
        } else if (operation.equals("U")) {
            if (!line.hasOption("i")) {
                return false;
            } else {
                try {
                    Long.parseLong(line.getOptionValue("i"));
                } catch (NumberFormatException ex) {
                    return false;
                }
            }
            if (!line.hasOption("n")) {
                return false;
            }
            if (line.hasOption("d")) {
                if (line.getOptionValue("d").length() > 512) {
                    return false;
                }
            }
            if (line.hasOption("a")) {
                try {
                    Long.parseLong(line.getOptionValue("a"));
                } catch (NumberFormatException ex) {
                    return false;
                }
            }
        } else if (operation.equals("D")) {
            if (!line.hasOption("i")) {
                return false;
            } else {
                try {
                    Long.parseLong(line.getOptionValue("i"));
                } catch (NumberFormatException ex) {
                    return false;
                }
            }
        } else if (operation.equals("A")) {
            // All records query
        } else if (operation.equals("N")) {
            // Number of records query
        } else {
            // nothing line C, R, U, D, A or N
            return false;
        }
    }
    return true;
}

From source file:ru.gkpromtech.exhibition.utils.JsonDateDeserializer.java

@Override
public Date deserialize(JsonParser jp, DeserializationContext ctx) throws IOException {
    // "2015-02-27T10:22:38.000Z"
    try {/*from   w w w  .j av  a 2 s . c o m*/
        return new Date(Long.parseLong(jp.getText()) * 1000);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:cz.muni.fi.pa165.creatures.rest.client.utils.DTOBuilder.java

public static AbstractDTO get(Class<?> clazz, CommandLine line) {
    if (clazz.equals(WeaponDTO.class)) {
        WeaponDTO dto = new WeaponDTO();
        if (line.hasOption("n")) {
            dto.setName(line.getOptionValue("n"));
        }//from   w ww  .j  av  a 2  s.  c om
        if (line.hasOption("m")) {
            dto.setAmmunition(new Integer(line.getOptionValue("m")));
        }
        if (line.hasOption("g")) {
            dto.setRange(new Integer(line.getOptionValue("g")));
        }
        if (line.hasOption("i")) {
            dto.setId(line.getOptionValue("i"));
        }
        return dto;
    } else if (clazz.equals(RegionDTO.class)) {
        RegionDTO dto = new RegionDTO();
        if (line.hasOption("n")) {
            dto.setName(line.getOptionValue("n"));
        }
        if (line.hasOption("d")) {
            dto.setDescription(line.getOptionValue("d"));
        }
        if (line.hasOption("a")) {
            dto.setArea(Long.parseLong(line.getOptionValue("a")));
        }
        if (line.hasOption("i")) {
            dto.setId(line.getOptionValue("i"));
        }
        return dto;
    } else {
        return null;
    }
}

From source file:blink.Message.java

/**
 * a Message object that represents a received message that will be processed from JSON
 * @param jsonReceived given JSON content of the message
 * @throws JSONException // ww w .j av  a2s  .c  o  m
 */
public Message(String jsonReceived) {
    JSONObject json;
    try {
        json = new JSONObject(jsonReceived);
        if (json.has("timestamp")) {
            timestamp = Long.parseLong((String) json.get("timestamp"));
        }
        if (json.has("content")) {
            text = (String) json.get("content");
        }
        userId = i++;
        try {
            type = (String) json.get("type");
        } catch (Exception e) {
            System.err.println("Error receiving message. No type in JSON received.");
            System.exit(-1);
        }
    } catch (JSONException ex) {
        Logger.getLogger(Message.class.getName()).log(Level.SEVERE, null, ex);
    }
    this.json = jsonReceived;

}

From source file:cloudworker.WorkerThread.java

WorkerThread(String task_id, String task, AmazonSQS sqs) {
    this.task_id = task_id;
    this.responseQName = task_id.split(":")[0].replaceAll("[^0-9]", "-");
    this.sleepLength = Long.parseLong(task);
    this.sqs = sqs;

    //System.out.println(responseQName);
}

From source file:com.hp.octane.integrations.testhelpers.OctaneSecuritySimulationUtils.java

static boolean authenticate(Request request) {
    if (request.getCookies() != null) {
        for (Cookie cookie : request.getCookies()) {
            if (SECURITY_COOKIE_NAME.equals(cookie.getName())) {
                String[] securityItems = cookie.getValue().split(SECURITY_TOKEN_SEPARATOR);
                long issuedAt = Long.parseLong(securityItems[2]);
                if (System.currentTimeMillis() - issuedAt > 2000) {
                    Cookie securityCookie = createSecurityCookie(securityItems[0], securityItems[1]);
                    request.getResponse().addCookie(securityCookie);
                }//from ww w  .  j  ava2  s. c  o  m
                return true;
            }
        }
    }
    request.getResponse().setStatus(HttpStatus.SC_UNAUTHORIZED);
    request.setHandled(true);
    return false;
}