package org.eclipse.core.internal.resources;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Map.Entry;
import org.eclipse.core.internal.utils.*;
import org.eclipse.core.runtime.Assert;
import org.eclipse.osgi.util.NLS;
public class MarkerInfo implements IMarkerSetElement, Cloneable, IStringPoolParticipant {
protected static final Integer INTEGER_ONE = 1;
protected static final Integer INTEGER_TWO = 2;
protected static final Integer INTEGER_ZERO = 0;
protected static final long UNDEFINED_ID = -1;
protected Map<String, Object> attributes = null;
protected long creationTime = 0;
protected long id = UNDEFINED_ID;
protected String type = null;
protected static Object checkValidAttribute(Object value) {
if (value == null)
return null;
if (value instanceof String) {
String valueString = (String) value;
if (valueString.length() < 21000)
return value;
byte[] bytes = valueString.getBytes(StandardCharsets.UTF_8);
if (bytes.length > 65535) {
String msg = "Marker property value is too long: " + valueString.substring(0, 10000);
Assert.isTrue(false, msg);
}
return value;
}
if (value instanceof Boolean) {
return ((Boolean) value).booleanValue() ? Boolean.TRUE : Boolean.FALSE;
}
if (value instanceof Integer) {
switch (((Integer) value).intValue()) {
case 0 :
return INTEGER_ZERO;
case 1 :
return INTEGER_ONE;
case 2 :
return INTEGER_TWO;
}
return value;
}
throw new IllegalArgumentException(NLS.bind(Messages.resources_wrongMarkerAttributeValueType, value.getClass().getName()));
}
public MarkerInfo() {
super();
}
@Override
public Object clone() {
try {
MarkerInfo copy = (MarkerInfo) super.clone();
copy.attributes = getAttributes(true);
return copy;
} catch (CloneNotSupportedException e) {
return null;
}
}
public Object getAttribute(String attributeName) {
return attributes == null ? null : attributes.get(attributeName);
}
public Map<String, Object> getAttributes() {
return getAttributes(true);
}
public Map<String, Object> getAttributes(boolean makeCopy) {
if (attributes == null)
return null;
return makeCopy ? new MarkerAttributeMap<>(attributes) : attributes;
}
public Object[] getAttributes(String[] attributeNames) {
Object[] result = new Object[attributeNames.length];
for (int i = 0; i < attributeNames.length; i++)
result[i] = getAttribute(attributeNames[i]);
return result;
}
public long getCreationTime() {
return creationTime;
}
@Override
public long getId() {
return id;
}
public String getType() {
return type;
}
public void internalSetAttributes(Map<String, Object> map) {
attributes = map;
}
public void setAttribute(String attributeName, Object value, boolean validate) {
if (validate)
value = checkValidAttribute(value);
if (attributes == null) {
if (value == null)
return;
attributes = new MarkerAttributeMap<>();
attributes.put(attributeName, value);
} else {
if (value == null) {
attributes.remove(attributeName);
if (attributes.isEmpty())
attributes = null;
} else {
attributes.put(attributeName, value);
}
}
}
public void setAttributes(Map<String, ? extends Object> map, boolean validate) {
if (map == null)
attributes = null;
else {
attributes = new MarkerAttributeMap<>(map.size());
for (Entry<String, ?> entry : map.entrySet()) {
Object key = entry.getKey();
Assert.isTrue(key instanceof String);
Object value = entry.getValue();
setAttribute((String) key, value, validate);
}
}
}
public void setAttributes(String[] attributeNames, Object[] values, boolean validate) {
Assert.isTrue(attributeNames.length == values.length);
for (int i = 0; i < attributeNames.length; i++)
setAttribute(attributeNames[i], values[i], validate);
}
public void setCreationTime(long value) {
creationTime = value;
}
public void setId(long value) {
id = value;
}
public void setType(String value) {
type = value;
}
@Override
public void shareStrings(StringPool set) {
type = set.add(type);
Map<String, Object> map = attributes;
if (map instanceof IStringPoolParticipant)
((IStringPoolParticipant) map).shareStrings(set);
}
}