/**
 * Copyright (c) 2014-2015 openHAB UG (haftungsbeschraenkt) 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.core.compat1x.internal;

import org.eclipse.smarthome.core.items.GroupItem;
import org.eclipse.smarthome.core.items.Item;
import org.eclipse.smarthome.core.library.items.ColorItem;
import org.eclipse.smarthome.core.library.items.ContactItem;
import org.eclipse.smarthome.core.library.items.DateTimeItem;
import org.eclipse.smarthome.core.library.items.DimmerItem;
import org.eclipse.smarthome.core.library.items.NumberItem;
import org.eclipse.smarthome.core.library.items.RollershutterItem;
import org.eclipse.smarthome.core.library.items.StringItem;
import org.eclipse.smarthome.core.library.items.SwitchItem;
import org.eclipse.smarthome.core.types.State;
import org.eclipse.smarthome.core.types.UnDefType;
import org.openhab.core.items.GenericItem;
import org.openhab.core.types.TypeParser;
import org.openhab.library.tel.items.ESHCallItem;

public class ItemMapper {

	public static org.openhab.core.items.Item mapToOpenHABItem(Item item) {
		org.openhab.core.items.Item result = null;

		if (item instanceof StringItem)
			result = new org.openhab.core.library.items.StringItem(item.getName());
		else if (item instanceof SwitchItem)
			result = new org.openhab.core.library.items.SwitchItem(item.getName());
		else if (item instanceof ContactItem)
			result = new org.openhab.core.library.items.ContactItem(item.getName());
		else if (item instanceof NumberItem)
			result = new org.openhab.core.library.items.NumberItem(item.getName());
		else if (item instanceof RollershutterItem)
			result = new org.openhab.core.library.items.RollershutterItem(item.getName());
		else if (item instanceof DimmerItem)
			result = new org.openhab.core.library.items.DimmerItem(item.getName());
		else if (item instanceof ColorItem)
			result = new org.openhab.core.library.items.ColorItem(item.getName());
		else if (item instanceof DateTimeItem)
			result = new org.openhab.core.library.items.DateTimeItem(item.getName());
		else if (item instanceof ESHCallItem)
			result = new org.openhab.library.tel.items.CallItem(item.getName());

		if (item instanceof GroupItem) {
			GroupItem gItem = (GroupItem) item;
			
			org.openhab.core.items.Item baseItem = ItemMapper.mapToOpenHABItem(gItem.getBaseItem());
			org.openhab.core.items.GroupItem ohgItem;
			
			if (baseItem instanceof GenericItem) {
				ohgItem = new org.openhab.core.items.GroupItem(item.getName(), (GenericItem) baseItem);
			} else {
				ohgItem = new org.openhab.core.items.GroupItem(item.getName());
			}
			
			for (Item member : gItem.getMembers()) {
				org.openhab.core.items.Item ohMember = ItemMapper.mapToOpenHABItem(member);
				if (ohMember != null) {
					ohgItem.addMember(ohMember);
				}
			}
			result = ohgItem;
		}

		if (result instanceof org.openhab.core.items.GenericItem) {
			org.openhab.core.items.GenericItem genericItem = (GenericItem) result;
			State state = item.getState();
			if (state != null) {
				org.openhab.core.types.State ohState = null;
				if (state==UnDefType.NULL) {
					ohState = org.openhab.core.types.UnDefType.NULL;
				} else if (state==UnDefType.UNDEF) {
					ohState = org.openhab.core.types.UnDefType.UNDEF;
				} else {
					ohState = TypeParser.parseState(genericItem.getAcceptedDataTypes(), state.toString());
				}
				if (ohState != null) {
					genericItem.setState(ohState);
				}
			}
		}
		
		return result;
	}

}

@@ -1332,39 +1332,95 @@
 = null;%0A
-%0A%09%09if (item instanceof 
+%09%09Class%3C? extends Item%3E itemClass = item.getClass();%0A%09%09%0A%09%09if (itemClass.equals(
 StringIt
@@ -1417,24 +1417,31 @@
 s(StringItem
+.class)
 )%0A%09%09%09result 
@@ -1520,28 +1520,29 @@
 if (item
- instanceof 
+Class.equals(
 SwitchIt
@@ -1539,24 +1539,31 @@
 s(SwitchItem
+.class)
 )%0A%09%09%09result 
@@ -1642,28 +1642,29 @@
 if (item
- instanceof 
+Class.equals(
 ContactI
@@ -1662,24 +1662,31 @@
 (ContactItem
+.class)
 )%0A%09%09%09result 
@@ -1766,28 +1766,29 @@
 if (item
- instanceof 
+Class.equals(
 NumberIt
@@ -1785,24 +1785,31 @@
 s(NumberItem
+.class)
 )%0A%09%09%09result 
@@ -1888,28 +1888,29 @@
 if (item
- instanceof 
+Class.equals(
 Rollersh
@@ -1914,24 +1914,31 @@
 rshutterItem
+.class)
 )%0A%09%09%09result 
@@ -2024,28 +2024,29 @@
 if (item
- instanceof 
+Class.equals(
 DimmerIt
@@ -2043,24 +2043,31 @@
 s(DimmerItem
+.class)
 )%0A%09%09%09result 
@@ -2146,28 +2146,29 @@
 if (item
- instanceof 
+Class.equals(
 ColorIte
@@ -2164,24 +2164,31 @@
 ls(ColorItem
+.class)
 )%0A%09%09%09result 
@@ -2266,28 +2266,29 @@
 if (item
- instanceof 
+Class.equals(
 DateTime
@@ -2287,24 +2287,31 @@
 DateTimeItem
+.class)
 )%0A%09%09%09result 
@@ -2392,28 +2392,29 @@
 if (item
- instanceof 
+Class.equals(
 ESHCallI
@@ -2416,16 +2416,23 @@
 CallItem
+.class)
 )%0A%09%09%09res

Use class equals instead of instanceof in ItemMapper