wwzeng1 / CoralME

A simple, fast and garbage-free matching engine order book that you can use as a starting point for your matching engines.
Apache License 2.0
0 stars 0 forks source link

Sweep: Move enums from Order.java to separate files and delete the enums from Order.java #1

Open wwzeng1 opened 1 month ago

wwzeng1 commented 1 month ago

Currently, several enums are defined within the Order.java file. To improve code organization and maintainability, move these enums to their own respective files.

Handle all imports and references in the files.

Enums to be moved (this is not an exhaustive list, please check for any additional enums): Remove the enums from the original Order.java file and move them to their own files.

sweep-nightly[bot] commented 3 weeks ago

🚀 Here's the PR! #38

Sweeping

Search Results

Response

I'll help move the enums from Order.java to their own files and update all references. Let's do this step by step.

First, let's create the new enum files:

src/main/java/com/coralblocks/coralme/enums/CancelReason.java

--- 
+++ 
@@ -1 +1,34 @@
+package com.coralblocks.coralme.enums;

+import com.coralblocks.coralme.util.CharEnum;
+import com.coralblocks.coralme.util.CharMap;
+
+public enum CancelReason implements CharEnum { 
+
+    MISSED          ('M'), 
+    USER            ('U'),
+    NO_LIQUIDITY    ('L'),
+    PRICE           ('E'),
+    CROSSED         ('C'),
+    PURGED          ('P'),
+    EXPIRED         ('D'),
+    ROLLED          ('R');
+
+    private final char b;
+    public final static CharMap<CancelReason> ALL = new CharMap<CancelReason>();
+    
+    static {
+        for(CancelReason cr : CancelReason.values()) {
+            if (ALL.put(cr.getChar(), cr) != null) throw new IllegalStateException("Duplicate: " + cr);
+        }
+    }
+    
+    private CancelReason(char b) {
+        this.b = b;
+    }
+    
+    @Override
+    public final char getChar() {
+        return b;
+    }
+}

src/main/java/com/coralblocks/coralme/enums/ExecuteSide.java

--- 
+++ 
@@ -1 +1,44 @@
+package com.coralblocks.coralme.enums;

+import com.coralblocks.coralme.util.CharEnum;
+import com.coralblocks.coralme.util.CharMap;
+import com.coralblocks.coralme.util.StringUtils;
+
+public enum ExecuteSide implements CharEnum {
+    
+    TAKER           ('T', "Y"),
+    MAKER           ('M', "N");
+    
+    private final char b;
+    private final String fixCode;
+    public final static CharMap<ExecuteSide> ALL = new CharMap<ExecuteSide>();
+    
+    static {
+        for(ExecuteSide es : ExecuteSide.values()) {
+            if (ALL.put(es.getChar(), es) != null) throw new IllegalStateException("Duplicate: " + es);
+        }
+    }
+    
+    private ExecuteSide(char b, String fixCode) {
+        this.b = b;
+        this.fixCode = fixCode;
+    }
+    
+    public static final ExecuteSide fromFixCode(CharSequence sb) {
+        for(ExecuteSide s : ExecuteSide.values()) {
+            if (StringUtils.equals(s.getFixCode(), sb)) {
+                return s;
+            }
+        }
+        return null;
+    }
+    
+    @Override
+    public final char getChar() {
+        return b;
+    }
+    
+    public final String getFixCode() {
+        return fixCode;
+    }
+}

src/main/java/com/coralblocks/coralme/enums/RejectReason.java

--- 
+++ 
@@ -1 +1,39 @@
+package com.coralblocks.coralme.enums;

+import com.coralblocks.coralme.util.CharEnum;
+import com.coralblocks.coralme.util.CharMap;
+
+public enum RejectReason implements CharEnum { 
+
+    MISSING_FIELD       ('1'),
+    BAD_TYPE           ('2'),
+    BAD_TIF            ('3'),
+    BAD_SIDE           ('4'),
+    BAD_SYMBOL         ('5'),
+    
+    BAD_PRICE          ('P'), 
+    BAD_SIZE           ('S'),
+    TRADING_HALTED     ('H'),
+    BAD_LOT            ('L'),
+    UNKNOWN_SYMBOL     ('U'),
+    DUPLICATE_EXCHANGE_ORDER_ID   ('E'),
+    DUPLICATE_CLIENT_ORDER_ID     ('C');
+
+    private final char b;
+    public final static CharMap<RejectReason> ALL = new CharMap<RejectReason>();
+    
+    static {
+        for(RejectReason rr : RejectReason.values()) {
+            if (ALL.put(rr.getChar(), rr) != null) throw new IllegalStateException("Duplicate: " + rr);
+        }
+    }
+    
+    private RejectReason(char b) {
+        this.b = b;
+    }
+    
+    @Override
+    public final char getChar() {
+        return b;
+    }
+}

src/main/java/com/coralblocks/coralme/enums/Side.java

--- 
+++ 
@@ -1 +1,74 @@
+package com.coralblocks.coralme.enums;

+import com.coralblocks.coralme.util.CharEnum;
+import com.coralblocks.coralme.util.CharMap;
+import com.coralblocks.coralme.util.StringUtils;
+
+public enum Side implements CharEnum { 
+
+    BUY             ('B', "1", 0), 
+    SELL            ('S', "2", 1);
+
+    private final char b;
+    private final String fixCode;
+    private final int index;
+    public final static CharMap<Side> ALL = new CharMap<Side>();
+    
+    static {
+        for(Side s : Side.values()) {
+            if (ALL.put(s.getChar(), s) != null) throw new IllegalStateException("Duplicate: " + s);
+        }
+        
+        if (ALL.size() != 2) {
+            throw new IllegalStateException("Side must have only two values: BUY and SELL!");
+        }
+    }
+    
+    private Side(char b, String fixCode, int index) {
+        this.b = b;
+        this.fixCode = fixCode;
+        this.index = index;
+    }
+    
+    public static final Side fromFixCode(CharSequence sb) {
+        for(Side s : Side.values()) {
+            if (StringUtils.equals(s.getFixCode(), sb)) {
+                return s;
+            }
+        }
+        return null;
+    }
+    
+    @Override
+    public final char getChar() {
+        return b;
+    }
+    
+    public final String getFixCode() {
+        return fixCode;
+    }
+
+    public final int index() {
+        return index;
+    }
+    
+    public final int invertedIndex() {
+        return this == BUY ? SELL.index() : BUY.index();
+    }
+    
+    public final boolean isBuy() {
+        return this == BUY;
+    }
+    
+    public final boolean isSell() {
+        return this == SELL;
+    }
+    
+    public final boolean isOutside(long price, long market) {
+        return this == BUY ? price < market : price > market;
+    }
+    
+    public final boolean isInside(long price, long market) {
+        return this == BUY ? price >= market : price <= market;
+    }
+}

src/main/java/com/coralblocks/coralme/enums/TimeInForce.java

--- 
+++ 
@@ -1 +1,45 @@
+package com.coralblocks.coralme.enums;

+import com.coralblocks.coralme.util.CharEnum;
+import com.coralblocks.coralme.util.CharMap;
+import com.coralblocks.coralme.util.StringUtils;
+
+public enum TimeInForce implements CharEnum { 
+
+    GTC             ('T', "1"), 
+    IOC             ('I', "3"),
+    DAY             ('D', "0");
+
+    private final char b;
+    private final String fixCode;
+    public final static CharMap<TimeInForce> ALL = new CharMap<TimeInForce>();
+    
+    static {
+        for(TimeInForce tif : TimeInForce.values()) {
+            if (ALL.put(tif.getChar(), tif) != null) throw new IllegalStateException("Duplicate: " + tif);
+        }
+    }
+    
+    private TimeInForce(char b, String fixCode) {
+        this.b = b;
+        this.fixCode = fixCode;
+    }
+    
+    public static final TimeInForce fromFixCode(CharSequence sb) {
+        for(TimeInForce s : TimeInForce.values()) {
+            if (StringUtils.equals(s.getFixCode(), sb)) {
+                return s;
+            }
+        }
+        return null;
+    }
+    
+    @Override
+    public final char getChar() {
+        return b;
+    }
+    
+    public final String getFixCode() {
+        return fixCode;
+    }
+}

src/main/java/com/coralblocks/coralme/enums/Type.java

--- 
+++ 
@@ -1 +1,44 @@
+package com.coralblocks.coralme.enums;

+import com.coralblocks.coralme.util.CharEnum;
+import com.coralblocks.coralme.util.CharMap;
+import com.coralblocks.coralme.util.StringUtils;
+
+public enum Type implements CharEnum { 
+
+    MARKET          ('M', "1"), 
+    LIMIT           ('L', "2");
+
+    private final char b;
+    private final String fixCode;
+    public final static CharMap<Type> ALL = new CharMap<Type>();
+    
+    static {
+        for(Type t : Type.values()) {
+            if (ALL.put(t.getChar(), t) != null) throw new IllegalStateException("Duplicate: " + t);
+        }
+    }
+    
+    private Type(char b, String fixCode) {
+        this.b = b;
+        this.fixCode = fixCode;
+    }
+    
+    public static final Type fromFixCode(CharSequence sb) {
+        for(Type s : Type.values()) {
+            if (StringUtils.equals(s.getFixCode(), sb)) {
+                return s;
+            }
+        }
+        return null;
+    }
+    
+    @Override
+    public final char getChar() {
+        return b;
+    }
+    
+    public final String getFixCode() {
+        return fixCode;
+    }
+}

Now let's update the Order.java file to remove the enums and update imports:

src/main/java/com/coralblocks/coralme/Order.java

--- 
+++ 
@@ -3,10 +3,8 @@
 import java.util.ArrayList;
 import java.util.List;

-import com.coralblocks.coralme.util.CharEnum;
-import com.coralblocks.coralme.util.CharMap;
+import com.coralblocks.coralme.enums.*;
 import com.coralblocks.coralme.util.DoubleUtils;
-import com.coralblocks.coralme.util.StringUtils;

 public class Order {

@@ -349,4 +347,3 @@

         listeners.clear();
     }
-    

src/main/java/com/coralblocks/coralme/Order.java

--- 
+++ 
@@ -111,197 +111,3 @@
            listeners.clear();
        }
     }
-    
-   public static enum TimeInForce implements CharEnum { 
-
-       GTC             ('T', "1"), 
-       IOC             ('I', "3"),
-       DAY             ('D', "0");
-
-       private final char b;
-       private final String fixCode;
-       public final static CharMap<TimeInForce> ALL = new CharMap<TimeInForce>();
-       
-       static {
-           for(TimeInForce tif : TimeInForce.values()) {
-               if (ALL.put(tif.getChar(), tif) != null) throw new IllegalStateException("Duplicate: " + tif);
-           }
-       }
-       
-       private TimeInForce(char b, String fixCode) {
-           this.b = b;
-           this.fixCode = fixCode;
-       }
-       
-       public static final TimeInForce fromFixCode(CharSequence sb) {
-           for(TimeInForce s : TimeInForce.values()) {
-               if (StringUtils.equals(s.getFixCode(), sb)) {
-                   return s;
-               }
-           }
-           return null;
-       }
-       
-       @Override
-        public final char getChar() {
-           return b;
-        }
-       
-       public final String getFixCode() {
-           return fixCode;
-       }
-   }
-   
-   public static enum RejectReason implements CharEnum { 
-
-       MISSING_FIELD       ('1'),
-       BAD_TYPE            ('2'),
-       BAD_TIF             ('3'),
-       BAD_SIDE            ('4'),
-       BAD_SYMBOL          ('5'),
-       
-       BAD_PRICE           ('P'), 
-       BAD_SIZE            ('S'),
-       TRADING_HALTED      ('H'),
-       BAD_LOT             ('L'),
-       UNKNOWN_SYMBOL      ('U'),
-       DUPLICATE_EXCHANGE_ORDER_ID ('E'),
-       DUPLICATE_CLIENT_ORDER_ID ('C');
-
-       private final char b;
-       public final static CharMap<RejectReason> ALL = new CharMap<RejectReason>();
-       
-       static {
-           for(RejectReason rr : RejectReason.values()) {
-               if (ALL.put(rr.getChar(), rr) != null) throw new IllegalStateException("Duplicate: " + rr);
-           }
-       }
-       
-       private RejectReason(char b) {
-           this.b = b;
-       }
-       
-       @Override
-        public final char getChar() {
-           return b;
-        }
-   }
-
-   public static enum CancelRejectReason implements CharEnum {
-       
-       NOT_FOUND       ('F');
-       
-       private final char b;
-       public final static CharMap<CancelRejectReason> ALL = new CharMap<CancelRejectReason>();
-       
-       static {
-           for(CancelRejectReason crr : CancelRejectReason.values()) {
-               if (ALL.put(crr.getChar(), crr) != null) throw new IllegalStateException("Duplicate: " + crr);
-           }
-       }
-       
-       private CancelRejectReason(char b) {
-           this.b = b;
-       }
-       
-       @Override
-        public final char getChar() {
-           return b;
-        }
-   }
-   
-   public static enum CancelReason implements CharEnum { 
-
-       MISSED          ('M'), 
-       USER            ('U'),
-       NO_LIQUIDITY    ('L'),
-       PRICE           ('E'),
-       CROSSED         ('C'),
-       PURGED          ('P'),
-       EXPIRED         ('D'),
-       ROLLED          ('R');
-
-       private final char b;
-       public final static CharMap<CancelReason> ALL = new CharMap<CancelReason>();
-       
-       static {
-           for(CancelReason cr : CancelReason.values()) {
-               if (ALL.put(cr.getChar(), cr) != null) throw new IllegalStateException("Duplicate: " + cr);
-           }
-       }
-       
-       private CancelReason(char b) {
-           this.b = b;
-       }
-       
-       @Override
-        public final char getChar() {
-           return b;
-        }
-   }
-   
-   public static enum ReduceRejectReason implements CharEnum { 
-
-       ZERO            ('Z'), 
-       NEGATIVE        ('N'),
-       INCREASE        ('I'),
-       SUPERFLUOUS     ('S'),
-       NOT_FOUND       ('F');
-
-       private final char b;
-       public final static CharMap<ReduceRejectReason> ALL = new CharMap<ReduceRejectReason>();
-       
-       static {
-           for(ReduceRejectReason rrr : ReduceRejectReason.values()) {
-               if (ALL.put(rrr.getChar(), rrr) != null) throw new IllegalStateException("Duplicate: " + rrr);
-           }
-       }
-       
-       private ReduceRejectReason(char b) {
-           this.b = b;
-       }
-       
-       @Override
-        public final char getChar() {
-           return b;
-        }
-   }
-   
-   public static enum Type implements CharEnum { 
-
-       MARKET          ('M', "1"), 
-       LIMIT           ('L', "2");
-
-       private final char b;
-       private final String fixCode;
-       public final static CharMap<Type> ALL = new CharMap<Type>();
-       
-       static {
-           for(Type t : Type.values()) {
-               if (ALL.put(t.getChar(), t) != null) throw new IllegalStateException("Duplicate: " + t);
-           }
-       }
-       
-       private Type(char b, String fixCode) {
-           this.b = b;
-           this.fixCode = fixCode;
-       }
-       
-       public static final Type fromFixCode(CharSequence sb) {
-           for(Type s : Type.values()) {
-               if (StringUtils.equals(s.getFixCode(), sb)) {
-                   return s;
-               }
-           }
-           return null;
-       }
-       
-       @Override
-        public final char getChar() {
-           return b;
-        }
-       
-       public final String getFixCode() {
-           return fixCode;
-       }
-   }

<code

Continue the conversation with Sweep here: http://localhost:3000/c/1b87bbf7-6d14-4254-bbe2-b9237f929c26.

:book: For more information on how to use Sweep, please read our documentation.

Tracking ID: dev-bb7b0350d1