Ticket #4142: flags.patch

File flags.patch, 6.4 KB (added by stoecker, 7 months ago)

Example private flags patch. Quick and Dirty.

  • src/org/openstreetmap/josm/data/osm/AbstractPrimitive.java

     
    33
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
     6import java.io.IOException;
     7import java.io.ObjectInputStream;
     8import java.io.ObjectOutputStream;
     9
    610import java.text.MessageFormat;
    711import java.time.Instant;
    812import java.util.ArrayList;
     
    132136
    133137    /**
    134138     * Put several boolean flags to one short int field to save memory.
    135      * Other bits of this field are used in subclasses.
    136139     */
    137     protected volatile short flags = FLAG_VISIBLE;   // visible per default
     140    private volatile short flags = FLAG_VISIBLE;   // visible per default
    138141
    139142    /**
    140143     * The mappaint cache index for this primitive.
     
    365368        return oldFlags != flags;
    366369    }
    367370
     371    protected void storeFlags(ObjectOutputStream oos) throws IOException {
     372        oos.writeShort(flags);
     373    }
     374
     375    protected void restoreFlags(ObjectInputStream ios) throws IOException {
     376        flags = ios.readShort();
     377    }
     378
    368379    @Override
    369380    public void setModified(boolean modified) {
    370381        updateFlags(FLAG_MODIFIED, modified);
     
    418429    }
    419430
    420431    @Override
     432    public boolean hasDirectionKeys() {
     433        return (flags & FLAG_HAS_DIRECTIONS) != 0;
     434    }
     435
     436    @Override
     437    public boolean reversedDirection() {
     438        return (flags & FLAG_DIRECTION_REVERSED) != 0;
     439    }
     440
     441    @Override
     442    public boolean isTagged() {
     443        return (flags & FLAG_TAGGED) != 0;
     444    }
     445
     446    @Override
     447    public boolean isAnnotated() {
     448        return (flags & FLAG_ANNOTATED) != 0;
     449    }
     450
     451    @Override
     452    public boolean isHighlighted() {
     453        return (flags & FLAG_HIGHLIGHTED) != 0;
     454    }
     455
     456    @Override
     457    public boolean isDisabled() {
     458        return (flags & FLAG_DISABLED) != 0;
     459    }
     460
     461    @Override
     462    public boolean isDisabledAndHidden() {
     463        return ((flags & FLAG_DISABLED) != 0) && ((flags & FLAG_HIDE_IF_DISABLED) != 0);
     464    }
     465
     466    @Override
     467    public boolean isPreserved() {
     468        return (flags & FLAG_PRESERVED) != 0;
     469    }
     470
     471    @Override
    421472    public boolean isIncomplete() {
    422473        return (flags & FLAG_INCOMPLETE) != 0;
    423474    }
     
    458509        updateFlags(FLAG_HIDDEN_TYPE, isExplicit);
    459510    }
    460511
     512    public void copyFlagsFrom(AbstractPrimitive old) {
     513        flags = old.flags;
     514    }
     515
    461516    @Override
    462517    public boolean isDrawable() {
    463518        return (flags & (FLAG_DELETED + FLAG_INCOMPLETE + FLAG_HIDE_IF_DISABLED)) == 0;
  • src/org/openstreetmap/josm/data/osm/OsmPrimitive.java

     
    364364    }
    365365
    366366    @Override
    367     public boolean isDisabled() {
    368         return (flags & FLAG_DISABLED) != 0;
    369     }
    370 
    371     @Override
    372     public boolean isDisabledAndHidden() {
    373         return ((flags & FLAG_DISABLED) != 0) && ((flags & FLAG_HIDE_IF_DISABLED) != 0);
    374     }
    375 
    376     @Override
    377     public boolean isPreserved() {
    378         return (flags & FLAG_PRESERVED) != 0;
    379     }
    380 
    381     @Override
    382367    public boolean isSelectable() {
    383368        // not synchronized -> check disabled twice just to be sure we did not have a race condition.
    384369        return !isDisabled() && isDrawable() && !isDisabled();
     
    492477        }
    493478    }
    494479
    495     @Override
    496     public boolean isHighlighted() {
    497         return (flags & FLAG_HIGHLIGHTED) != 0;
    498     }
    499 
    500480    /*---------------
    501481     * DIRECTION KEYS
    502482     *---------------*/
     
    526506        updateFlagsNoLock(FLAG_ANNOTATED, hasKeys() && getWorkInProgressKeys().stream().anyMatch(this::hasKey));
    527507    }
    528508
    529     @Override
    530     public boolean isTagged() {
    531         return (flags & FLAG_TAGGED) != 0;
    532     }
    533 
    534     @Override
    535     public boolean isAnnotated() {
    536         return (flags & FLAG_ANNOTATED) != 0;
    537     }
    538 
    539509    protected void updateDirectionFlags() {
    540510        boolean hasDirections = false;
    541511        boolean directionReversed = false;
     
    551521        updateFlagsNoLock(FLAG_HAS_DIRECTIONS, hasDirections);
    552522    }
    553523
    554     @Override
    555     public boolean hasDirectionKeys() {
    556         return (flags & FLAG_HAS_DIRECTIONS) != 0;
    557     }
    558 
    559     @Override
    560     public boolean reversedDirection() {
    561         return (flags & FLAG_DIRECTION_REVERSED) != 0;
    562     }
    563 
    564524    /*------------
    565525     * Keys handling
    566526     ------------*/
     
    854814            setKeys(other.hasKeys() ? other.getKeys() : null);
    855815            timestamp = other.timestamp;
    856816            version = other.version;
    857             setIncomplete(other.isIncomplete());
    858             flags = other.flags;
     817            setIncomplete(other.isIncomplete()); /* hmm, why, it's done by copyFlagsFrom() */
     818            copyFlagsFrom(other);
    859819            user = other.user;
    860820            changesetId = other.changesetId;
    861821        } finally {
  • src/org/openstreetmap/josm/data/osm/PrimitiveData.java

     
    9090        oos.writeInt(changesetId);
    9191        oos.writeInt(timestamp);
    9292        oos.writeObject(keys);
    93         oos.writeShort(flags);
     93        storeFlags(oos);
    9494        oos.defaultWriteObject();
    9595    }
    9696
     
    103103        changesetId = ois.readInt();
    104104        timestamp = ois.readInt();
    105105        keys = (String[]) ois.readObject();
    106         flags = ois.readShort();
     106        restoreFlags(ois);
    107107        ois.defaultReadObject();
    108108    }
    109109
  • src/org/openstreetmap/josm/data/vector/VectorPrimitive.java

     
    5959    }
    6060
    6161    @Override
    62     public boolean isTagged() {
    63         return (flags & FLAG_TAGGED) != 0;
    64     }
    65 
    66     @Override
    6762    public boolean isAnnotated() {
    6863        return this.getInterestingTags().size() - this.getKeys().size() > 0;
    6964    }