attributes cleanup

This commit is contained in:
Alexey Kudravtsev
2013-09-23 17:48:13 +04:00
parent 75b3eaac7f
commit 97cd63393f
2 changed files with 71 additions and 82 deletions
@@ -19,6 +19,7 @@
*/
package com.intellij.openapi.editor.markup;
import com.intellij.openapi.util.Comparing;
import com.intellij.util.ConcurrencyUtil;
import com.intellij.util.containers.StripedLockConcurrentHashMap;
import org.intellij.lang.annotations.JdkConstants;
@@ -28,17 +29,29 @@ import org.jetbrains.annotations.NotNull;
import java.awt.*;
public class AttributesFlyweight {
private final int myHashCode;
private static final StripedLockConcurrentHashMap<FlyweightKey, AttributesFlyweight> entries = new StripedLockConcurrentHashMap<FlyweightKey, AttributesFlyweight>();
private static final ThreadLocal<FlyweightKey> ourKey = new ThreadLocal<FlyweightKey>();
private final int myHashCode;
private final Color myForeground;
private final Color myBackground;
@JdkConstants.FontStyle
private final int myFontType;
private final Color myEffectColor;
private final EffectType myEffectType;
private final Color myErrorStripeColor;
private static class FlyweightKey implements Cloneable {
Color foreground;
Color background;
@JdkConstants.FontStyle int fontType;
Color effectColor;
EffectType effectType;
Color errorStripeColor;
private Color foreground;
private Color background;
@JdkConstants.FontStyle
private int fontType;
private Color effectColor;
private EffectType effectType;
private Color errorStripeColor;
private FlyweightKey() {
}
@Override
public boolean equals(Object o) {
@@ -59,13 +72,7 @@ public class AttributesFlyweight {
@Override
public int hashCode() {
int result = foreground != null ? foreground.hashCode() : 0;
result = 31 * result + (background != null ? background.hashCode() : 0);
result = 31 * result + fontType;
result = 31 * result + (effectColor != null ? effectColor.hashCode() : 0);
result = 31 * result + (effectType != null ? effectType.hashCode() : 0);
result = 31 * result + (errorStripeColor != null ? errorStripeColor.hashCode() : 0);
return result;
return calcHashCode(foreground, background, fontType, effectColor, effectType, errorStripeColor);
}
@Override
@@ -102,39 +109,37 @@ public class AttributesFlyweight {
return flyweight;
}
return ConcurrencyUtil.cacheOrGet(entries, key.clone(), new AttributesFlyweight(foreground, background, fontType, effectColor, effectType, errorStripeColor));
AttributesFlyweight newValue = new AttributesFlyweight(foreground, background, fontType, effectColor, effectType, errorStripeColor);
return ConcurrencyUtil.cacheOrGet(entries, key.clone(), newValue);
}
private final Color myForeground;
private final Color myBackground;
@JdkConstants.FontStyle
private final int myFontType;
private final Color myEffectColor ;
private final EffectType myEffectType;
private final Color myErrorStripeColor;
private AttributesFlyweight(Color foreground,
Color background,
@JdkConstants.FontStyle int fontType,
Color effectColor,
EffectType effectType,
Color errorStripeColor) {
Color background,
@JdkConstants.FontStyle int fontType,
Color effectColor,
EffectType effectType,
Color errorStripeColor) {
myForeground = foreground;
myBackground = background;
myFontType = fontType;
myEffectColor = effectColor;
myEffectType = effectType;
myErrorStripeColor = errorStripeColor;
myHashCode = calcHashCode();
myHashCode = calcHashCode(foreground, background, fontType, effectColor, effectType, errorStripeColor);
}
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);
private static int calcHashCode(Color foreground,
Color background,
int fontType,
Color effectColor,
EffectType effectType,
Color errorStripeColor) {
int result = foreground != null ? foreground.hashCode() : 0;
result = 31 * result + (background != null ? background.hashCode() : 0);
result = 31 * result + fontType;
result = 31 * result + (effectColor != null ? effectColor.hashCode() : 0);
result = 31 * result + (effectType != null ? effectType.hashCode() : 0);
result = 31 * result + (errorStripeColor != null ? errorStripeColor.hashCode() : 0);
return result;
}
@@ -163,28 +168,34 @@ public class AttributesFlyweight {
return myErrorStripeColor;
}
public AttributesFlyweight withForeground(Color fore) {
return create(fore, myBackground, myFontType, myEffectColor, myEffectType, myErrorStripeColor);
@NotNull
public AttributesFlyweight withForeground(Color foreground) {
return Comparing.equal(foreground, myForeground) ? this : create(foreground, myBackground, myFontType, myEffectColor, myEffectType, myErrorStripeColor);
}
public AttributesFlyweight withBackground(Color back) {
return create(myForeground, back, myFontType, myEffectColor, myEffectType, myErrorStripeColor);
@NotNull
public AttributesFlyweight withBackground(Color background) {
return Comparing.equal(background, myBackground) ? this : create(myForeground, background, myFontType, myEffectColor, myEffectType, myErrorStripeColor);
}
@NotNull
public AttributesFlyweight withFontType(@JdkConstants.FontStyle int fontType) {
return create(myForeground, myBackground, fontType, myEffectColor, myEffectType, myErrorStripeColor);
return fontType == myFontType ? this : create(myForeground, myBackground, fontType, myEffectColor, myEffectType, myErrorStripeColor);
}
@NotNull
public AttributesFlyweight withEffectColor(Color effectColor) {
return create(myForeground, myBackground, myFontType, effectColor, myEffectType, myErrorStripeColor);
return Comparing.equal(effectColor, myEffectColor) ? this : create(myForeground, myBackground, myFontType, effectColor, myEffectType, myErrorStripeColor);
}
@NotNull
public AttributesFlyweight withEffectType(EffectType effectType) {
return create(myForeground, myBackground, myFontType, myEffectColor, effectType, myErrorStripeColor);
return Comparing.equal(effectType, myEffectType) ? this : create(myForeground, myBackground, myFontType, myEffectColor, effectType, myErrorStripeColor);
}
@NotNull
public AttributesFlyweight withErrorStripeColor(Color stripeColor) {
return create(myForeground, myBackground, myFontType, myEffectColor, myEffectType, stripeColor);
return Comparing.equal(stripeColor, myErrorStripeColor) ? this : create(myForeground, myBackground, myFontType, myEffectColor, myEffectType, stripeColor);
}
@Override
@@ -212,19 +223,7 @@ public class AttributesFlyweight {
@NonNls
@Override
public String toString() {
return "AttributesFlyweight{" +
"myForeground=" +
myForeground +
", myBackground=" +
myBackground +
", myFontType=" +
myFontType +
", myEffectColor=" +
myEffectColor +
", myEffectType=" +
myEffectType +
", myErrorStripeColor=" +
myErrorStripeColor +
'}';
return "AttributesFlyweight{myForeground=" + myForeground + ", myBackground=" + myBackground + ", myFontType=" + myFontType +
", myEffectColor=" + myEffectColor + ", myEffectType=" + myEffectType + ", myErrorStripeColor=" + myErrorStripeColor + '}';
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2010 JetBrains s.r.o.
* Copyright 2000-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,9 +16,13 @@
package com.intellij.openapi.editor.markup;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.*;
import com.intellij.openapi.util.DefaultJDOMExternalizer;
import com.intellij.openapi.util.InvalidDataException;
import com.intellij.openapi.util.JDOMExternalizable;
import com.intellij.openapi.util.WriteExternalException;
import org.intellij.lang.annotations.JdkConstants;
import org.jdom.Element;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import java.awt.*;
@@ -34,6 +38,9 @@ public class TextAttributes implements JDOMExternalizable, Cloneable {
private boolean myEnforcedDefaults = false;
@NotNull
private AttributesFlyweight myAttrs;
/**
* Merges (layers) the two given text attributes.
*
@@ -41,6 +48,7 @@ public class TextAttributes implements JDOMExternalizable, Cloneable {
* @param above Text attributes to merge "above", overriding settings from "under".
* @return Merged attributes instance.
*/
@Contract("!null, !null -> !null")
public static TextAttributes merge(TextAttributes under, TextAttributes above) {
if (under == null) return above;
if (above == null) return under;
@@ -154,8 +162,6 @@ public class TextAttributes implements JDOMExternalizable, Cloneable {
}
}
private AttributesFlyweight myAttrs;
public TextAttributes() {
this(null, null, null, EffectType.BOXED, Font.PLAIN);
}
@@ -194,7 +200,7 @@ public class TextAttributes implements JDOMExternalizable, Cloneable {
}
@NotNull
public static TextAttributes fromFlyweight(AttributesFlyweight flyweight) {
public static TextAttributes fromFlyweight(@NotNull AttributesFlyweight flyweight) {
TextAttributes f = new TextAttributes();
f.myAttrs = flyweight;
return f;
@@ -277,12 +283,7 @@ public class TextAttributes implements JDOMExternalizable, Cloneable {
public void readExternal(Element element) throws InvalidDataException {
Externalizable ext = new Externalizable();
ext.readExternal(element);
myAttrs = AttributesFlyweight.create(ext.FOREGROUND,
ext.BACKGROUND,
ext.FONT_TYPE,
ext.EFFECT_COLOR,
ext.getEffectType(),
ext.ERROR_STRIPE_COLOR);
myAttrs = AttributesFlyweight.create(ext.FOREGROUND, ext.BACKGROUND, ext.FONT_TYPE, ext.EFFECT_COLOR, ext.getEffectType(), ext.ERROR_STRIPE_COLOR);
if (isEmpty()) myEnforcedDefaults = true;
}
@@ -302,18 +303,7 @@ public class TextAttributes implements JDOMExternalizable, Cloneable {
@Override
public String toString() {
return "[" +
getForegroundColor() +
"," +
getBackgroundColor() +
"," +
getFontType() +
"," +
getEffectType() +
"," +
getEffectColor() +
"," +
getErrorStripeColor() +
"]";
return "[" + getForegroundColor() + "," + getBackgroundColor() + "," + getFontType() + "," + getEffectType() + "," +
getEffectColor() + "," + getErrorStripeColor() + "]";
}
}