<commit_before>/*
 * Copyright (c) 2010-2016, openHAB.org and others.
 *
 *   All rights reserved. This program and the accompanying materials
 *   are made available under the terms of the Eclipse Public License v1.0
 *   which accompanies this distribution, and is available at
 *   http://www.eclipse.org/legal/epl-v10.html
 */

package org.openhab.habdroid.model;

import android.graphics.Color;
import android.util.Log;

import com.crittercism.app.Crittercism;

import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * This is a class to hold basic information about openHAB Item.
 */

public class OpenHABItem {
	private String name;
	private String type;
	private String groupType;
	private String state = "";
	private String link;
	private final static String TAG = OpenHABItem.class.getSimpleName();
	private final static Pattern HSB_PATTERN = Pattern.compile("^\\d+,\\d+,(\\d+)$");

	public OpenHABItem(Node startNode) {
		if (startNode.hasChildNodes()) {
			NodeList childNodes = startNode.getChildNodes();
			for (int i = 0; i < childNodes.getLength(); i ++) {
				Node childNode = childNodes.item(i);
				if (childNode.getNodeName().equals("type")) {
					this.setType(childNode.getTextContent());
				} else if (childNode.getNodeName().equals("groupType")) {
					this.setGroupType(childNode.getTextContent());
				} else if (childNode.getNodeName().equals("name")) {
					this.setName(childNode.getTextContent());
				} else if (childNode.getNodeName().equals("state")) {
					if (childNode.getTextContent().equals("Uninitialized")) {
						this.setState(null);
					} else {
						this.setState(childNode.getTextContent());
					}
				} else if (childNode.getNodeName().equals("link")) {					
					this.setLink(childNode.getTextContent());
				}
			}
		}
	}

    public OpenHABItem(JSONObject jsonObject) {
            try {
                if (jsonObject.has("type"))
                    this.setType(jsonObject.getString("type"));
				if (jsonObject.has("groupType"))
					this.setGroupType(jsonObject.getString("groupType"));
                if (jsonObject.has("name"))
                    this.setName(jsonObject.getString("name"));
                if (jsonObject.has("state")) {
                    if (jsonObject.getString("state").equals("NULL") ||
                            jsonObject.getString("state").equals("UNDEF") ||
                            jsonObject.getString("state").equalsIgnoreCase("undefined")) {
                        this.setState(null);
                    } else {
                        this.setState(jsonObject.getString("state"));
                    }
                }
                if (jsonObject.has("link"))
                    this.setLink(jsonObject.getString("link"));
            } catch (JSONException e) {
                e.printStackTrace();
            }
    }
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getGroupType() {
		return groupType;
	}

	public void setGroupType(String groupType) {
		this.groupType = groupType;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public boolean getStateAsBoolean() {
		// For uninitialized/null state return false
		if (state == null) {
			return false;
		}
		// If state is ON for switches return True
		if (state.equals("ON")) {
			return true;
		}

		Matcher hsbMatcher = HSB_PATTERN.matcher(state);
		if(hsbMatcher.find()) {
			String brightness = hsbMatcher.group(1);
			return isValueDecimalIntegerAndGreaterThanZero(brightness);
		}

		return isValueDecimalIntegerAndGreaterThanZero(state);

	}

	private Boolean isValueDecimalIntegerAndGreaterThanZero(String value) {
		try {
			int decimalValue = Integer.valueOf(value);
			return decimalValue > 0;
		} catch (NumberFormatException e) {
			return false;
		}
	}

	public Float getStateAsFloat() {
		Float result;
		// For uninitialized/null state return zero
		if (state == null) {
			result = new Float(0);
		} else if ("ON".equals(state)) {
			result = new Float(100);
		} else if ("OFF".equals(state)) {
			result = new Float(0);
		} else {
			try {
				result = Float.parseFloat(state);
			} catch (NumberFormatException e) {
				if (e != null) {
					Crittercism.logHandledException(e);
					Log.e(TAG, e.getMessage());
				}
				result = new Float(0);
			}
		}
		return result;
	}

	public float[] getStateAsHSV() {
		if (state == null) {<commit_msg>simpler creation of floats (#204)<commit_after>/*
 * Copyright (c) 2010-2016, openHAB.org and others.
 *
 *   All rights reserved. This program and the accompanying materials
 *   are made available under the terms of the Eclipse Public License v1.0
 *   which accompanies this distribution, and is available at
 *   http://www.eclipse.org/legal/epl-v10.html
 */

package org.openhab.habdroid.model;

import android.graphics.Color;
import android.util.Log;

import com.crittercism.app.Crittercism;

import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * This is a class to hold basic information about openHAB Item.
 */

public class OpenHABItem {
	private String name;
	private String type;
	private String groupType;
	private String state = "";
	private String link;
	private final static String TAG = OpenHABItem.class.getSimpleName();
	private final static Pattern HSB_PATTERN = Pattern.compile("^\\d+,\\d+,(\\d+)$");

	public OpenHABItem(Node startNode) {
		if (startNode.hasChildNodes()) {
			NodeList childNodes = startNode.getChildNodes();
			for (int i = 0; i < childNodes.getLength(); i ++) {
				Node childNode = childNodes.item(i);
				if (childNode.getNodeName().equals("type")) {
					this.setType(childNode.getTextContent());
				} else if (childNode.getNodeName().equals("groupType")) {
					this.setGroupType(childNode.getTextContent());
				} else if (childNode.getNodeName().equals("name")) {
					this.setName(childNode.getTextContent());
				} else if (childNode.getNodeName().equals("state")) {
					if (childNode.getTextContent().equals("Uninitialized")) {
						this.setState(null);
					} else {
						this.setState(childNode.getTextContent());
					}
				} else if (childNode.getNodeName().equals("link")) {					
					this.setLink(childNode.getTextContent());
				}
			}
		}
	}

    public OpenHABItem(JSONObject jsonObject) {
            try {
                if (jsonObject.has("type"))
                    this.setType(jsonObject.getString("type"));
				if (jsonObject.has("groupType"))
					this.setGroupType(jsonObject.getString("groupType"));
                if (jsonObject.has("name"))
                    this.setName(jsonObject.getString("name"));
                if (jsonObject.has("state")) {
                    if (jsonObject.getString("state").equals("NULL") ||
                            jsonObject.getString("state").equals("UNDEF") ||
                            jsonObject.getString("state").equalsIgnoreCase("undefined")) {
                        this.setState(null);
                    } else {
                        this.setState(jsonObject.getString("state"));
                    }
                }
                if (jsonObject.has("link"))
                    this.setLink(jsonObject.getString("link"));
            } catch (JSONException e) {
                e.printStackTrace();
            }
    }
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getGroupType() {
		return groupType;
	}

	public void setGroupType(String groupType) {
		this.groupType = groupType;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public boolean getStateAsBoolean() {
		// For uninitialized/null state return false
		if (state == null) {
			return false;
		}
		// If state is ON for switches return True
		if (state.equals("ON")) {
			return true;
		}

		Matcher hsbMatcher = HSB_PATTERN.matcher(state);
		if(hsbMatcher.find()) {
			String brightness = hsbMatcher.group(1);
			return isValueDecimalIntegerAndGreaterThanZero(brightness);
		}

		return isValueDecimalIntegerAndGreaterThanZero(state);

	}

	private Boolean isValueDecimalIntegerAndGreaterThanZero(String value) {
		try {
			int decimalValue = Integer.valueOf(value);
			return decimalValue > 0;
		} catch (NumberFormatException e) {
			return false;
		}
	}

	public Float getStateAsFloat() {
		Float result;
		// For uninitialized/null state return zero
		if (state == null) {
			result = 0f;
		} else if ("ON".equals(state)) {
			result = 100f;
		} else if ("OFF".equals(state)) {
			result = 0f;
		} else {
			try {
				result = Float.parseFloat(state);
			} catch (NumberFormatException e) {
				if (e != null) {
					Crittercism.logHandledException(e);
					Log.e(TAG, e.getMessage());
				}
				result = new Float(0);
			}
		}
		return result;
	}

	public float[] getStateAsHSV() {
		if (state == null) {
{'fraction_non_alphanumeric': 0.1279787234042553, 'fraction_numerical': 0.005212765957446809, 'mean_word_length': 2.824654190398698, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5043696', 'n_tokens_mistral': 3209, 'n_tokens_neox': 2806, 'n_words': 633}