performance

This commit is contained in:
Alexey Kudravtsev
2011-02-10 13:29:35 +03:00
parent d4117de40b
commit 1d80ace6db
2 changed files with 38 additions and 75 deletions
@@ -19,24 +19,16 @@
*/
package com.intellij.openapi.editor.markup;
import gnu.trove.THashSet;
import com.intellij.util.ConcurrencyUtil;
import com.intellij.util.containers.StripedLockConcurrentHashMap;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import java.awt.*;
public class AttributesFlyweight {
private static final class MyTHashSet extends THashSet<AttributesFlyweight> {
public int index(final AttributesFlyweight obj) {
return super.index(obj);
}
public AttributesFlyweight get(int index) {
return (AttributesFlyweight)_set[index];
}
}
private static final MyTHashSet entries = new MyTHashSet();
private final int myHashCode;
private static final StripedLockConcurrentHashMap<AttributesFlyweight, AttributesFlyweight> entries = new StripedLockConcurrentHashMap<AttributesFlyweight, AttributesFlyweight>();
@NotNull
public static AttributesFlyweight create(Color foreground,
@@ -46,15 +38,7 @@ public class AttributesFlyweight {
EffectType effectType,
Color errorStripeColor) {
AttributesFlyweight key = new AttributesFlyweight(foreground, background, fontType, effectColor, effectType, errorStripeColor);
synchronized (entries) {
int idx = entries.index(key);
if (idx >= 0) {
return entries.get(idx);
}
entries.add(key);
return key;
}
return ConcurrencyUtil.cacheOrGet(entries, key, key);
}
private final Color myForeground;
@@ -64,18 +48,29 @@ public class AttributesFlyweight {
private final EffectType myEffectType;
private final Color myErrorStripeColor;
AttributesFlyweight(Color foreground,
Color background,
int fontType,
Color effectColor,
EffectType effectType,
Color errorStripeColor) {
private AttributesFlyweight(Color foreground,
Color background,
int fontType,
Color effectColor,
EffectType effectType,
Color errorStripeColor) {
myForeground = foreground;
myBackground = background;
myFontType = fontType;
myEffectColor = effectColor;
myEffectType = effectType;
myErrorStripeColor = errorStripeColor;
myHashCode = calcHashCode();
}
private int calcHashCode() {
int result = myForeground != null ? myForeground.hashCode() : 0;
result = 31 * result + (myBackground != null ? myBackground.hashCode() : 0);
result = 31 * result + myFontType;
result = 31 * result + (myEffectColor != null ? myEffectColor.hashCode() : 0);
result = 31 * result + (myEffectType != null ? myEffectType.hashCode() : 0);
result = 31 * result + (myErrorStripeColor != null ? myErrorStripeColor.hashCode() : 0);
return result;
}
public Color getForeground() {
@@ -145,13 +140,7 @@ public class AttributesFlyweight {
@Override
public int hashCode() {
int result = myForeground != null ? myForeground.hashCode() : 0;
result = 31 * result + (myBackground != null ? myBackground.hashCode() : 0);
result = 31 * result + myFontType;
result = 31 * result + (myEffectColor != null ? myEffectColor.hashCode() : 0);
result = 31 * result + (myEffectType != null ? myEffectType.hashCode() : 0);
result = 31 * result + (myErrorStripeColor != null ? myErrorStripeColor.hashCode() : 0);
return result;
return myHashCode;
}
@NonNls
@@ -111,17 +111,23 @@ public class TextAttributes implements JDOMExternalizable, Cloneable {
private void setEffectType(EffectType effectType) {
if (effectType == EffectType.BOXED) {
EFFECT_TYPE = EFFECT_BORDER;
} else if (effectType == EffectType.LINE_UNDERSCORE) {
}
else if (effectType == EffectType.LINE_UNDERSCORE) {
EFFECT_TYPE = EFFECT_LINE;
} else if (effectType == EffectType.BOLD_LINE_UNDERSCORE) {
}
else if (effectType == EffectType.BOLD_LINE_UNDERSCORE) {
EFFECT_TYPE = EFFECT_BOLD_LINE;
} else if (effectType == EffectType.STRIKEOUT) {
}
else if (effectType == EffectType.STRIKEOUT) {
EFFECT_TYPE = EFFECT_STRIKEOUT;
} else if (effectType == EffectType.WAVE_UNDERSCORE) {
}
else if (effectType == EffectType.WAVE_UNDERSCORE) {
EFFECT_TYPE = EFFECT_WAVE;
} else if (effectType == EffectType.BOLD_DOTTED_LINE) {
}
else if (effectType == EffectType.BOLD_DOTTED_LINE) {
EFFECT_TYPE = EFFECT_BOLD_DOTTED_LINE;
} else {
}
else {
EFFECT_TYPE = -1;
}
}
@@ -213,44 +219,12 @@ public class TextAttributes implements JDOMExternalizable, Cloneable {
if(!(obj instanceof TextAttributes)) {
return false;
}
TextAttributes textAttributes = (TextAttributes)obj;
if(!Comparing.equal(textAttributes.getForegroundColor(), getForegroundColor())) {
return false;
}
if(!Comparing.equal(textAttributes.getBackgroundColor(), getBackgroundColor())) {
return false;
}
if(!Comparing.equal(textAttributes.getErrorStripeColor(), getErrorStripeColor())) {
return false;
}
if(!Comparing.equal(textAttributes.getEffectColor(), getEffectColor())) {
return false;
}
if (textAttributes.getEffectType() != getEffectType()) {
return false;
}
if(textAttributes.getFontType() != getFontType()) {
return false;
}
return true;
// myAttrs are interned, see com.intellij.openapi.editor.markup.AttributesFlyweight.create()
return myAttrs == ((TextAttributes)obj).myAttrs;
}
public int hashCode() {
int hashCode = 0;
if(getForegroundColor() != null) {
hashCode += getForegroundColor().hashCode();
}
if(getBackgroundColor() != null) {
hashCode += getBackgroundColor().hashCode();
}
if(getErrorStripeColor() != null) {
hashCode += getErrorStripeColor().hashCode();
}
if(getEffectColor() != null) {
hashCode += getEffectColor().hashCode();
}
hashCode += getFontType();
return hashCode;
return myAttrs.hashCode();
}
public void readExternal(Element element) throws InvalidDataException {