Transcribe some Java code to C#

Hi Ladoes and Gents,

I am very new to Java and had a little code I would love some help with.  I basically need to get the associative C# version of this Java code.  Please if you could comment the heck out of the code to describe what it does on the c# versioni as well it would be greatly appreciated.

FYI I have tried to install the java to C# conversion assistant on a few different machines with different versions of visual studio with no luck.  I get an error message so a automated conversion is out.

Let me please relay my gracious thank you in advance.  I hope everyone is well.

Best - Dynotoe
import java.io.File;
import java.util.Calendar;
import java.util.TimeZone;

public class DataCacheUtils {
    public static enum Instrument {
        AUDJPY, AUDNZD, AUDUSD, CADJPY, CHFJPY, EURAUD, EURCAD, EURCHF, EURDKK, EURGBP, EURHKD, EURJPY, EURNOK, EURSEK, EURUSD, GBPCHF, GBPJPY,
        GBPUSD, NZDUSD, USDCAD, USDCHF, USDDKK, USDHKD, USDJPY, USDMXN, USDNOK, USDSEK, USDSGD, USDTRY;
    }

    public enum OfferSide {
        BID, ASK;
    }

    public static enum Unit {
        Second(1000L, "s", "Sec", "Second"),
        Minute(60 * 1000L, "m", "Min", "Minute"),
        Hour(60 * 60 * 1000L, "h", "Hour", "Hourly"),
        Day(24 * 60 * 60 * 1000L, "D", "Day", "Daily"),
        Week(7 * 24 * 60 * 60 * 1000L, "W", "Week", "Weekly"),
        Month(30 * 24 * 60 * 60 * 1000L, "M", "Month", "Monthly");

        private long interval;
        private Unit(long interval, String shortDescription, String compactDescription, String longDescription) {
            this.interval = interval;
        }
        public final long getInterval() {
            return interval;
        }
    }
    public static enum Period {
        TICK(null, -1),
        TEN_SECS(Unit.Second, 10),
        ONE_MIN(Unit.Minute, 1),
        FIVE_MINS(Unit.Minute, 5),
        TEN_MINS(Unit.Minute, 10),
        FIFTEEN_MINS(Unit.Minute, 15),
        THIRTY_MINS(Unit.Minute, 30),
        ONE_HOUR(Unit.Hour, 1),
        FOUR_HOURS(Unit.Hour, 4),
        DAILY(Unit.Day, 1),
        WEEKLY(Unit.Week, 1),
        MONTHLY(Unit.Month, 1);

        public final Unit unit;

        public final int numOfUnits;

        private Period(Unit unit, int nUnits) {
            this.unit = unit;
            this.numOfUnits = nUnits;
        }

        public final long getInterval() {
            return this == TICK ? -1 : unit.getInterval() * numOfUnits;
        }
    }
    
    public static final long getChunkStart(final Period period, final long time) {
        if (period == Period.TICK) {
            return getClosestHourStartBefore(time);
        } else {
            switch (period.unit) {
                case Second:
                case Minute:
                    return getClosestDayStartBefore(time);
                case Hour:
                case Day:
                    return getClosestMonthStartBefore(time);
                case Week:
                case Month:
                    return getClosestYearStartBefore(time);
                default:
                    throw new RuntimeException("Period not supported");
            }
        }
    }

    public static final long getChunkEnd(final Period period, final long time) {
        final long chunkStart = getChunkStart(period, time);
        if (period == Period.TICK) {
            return chunkStart + 60 * 60 * 1000;
        } else {
            switch (period.unit) {
                case Second:
                case Minute:
                    return chunkStart + 24 * 60 * 60 * 1000 - period.getInterval();
                case Hour:
                case Day:
                    Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
                    cal.setTimeInMillis(chunkStart);
                    cal.add(Calendar.MONTH, 1);
                    return cal.getTimeInMillis() - period.getInterval();
                case Week:
                    cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
                    cal.setTimeInMillis(chunkStart);
                    cal.add(Calendar.YEAR, 1);
                    long yearEnd = cal.getTimeInMillis();
                    cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
                    if (yearEnd <= cal.getTimeInMillis()) {
                        cal.add(Calendar.WEEK_OF_YEAR, -1);
                    }
                    return cal.getTimeInMillis();
                case Month:
                    cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
                    cal.setTimeInMillis(chunkStart);
                    cal.add(Calendar.YEAR, 1);
                    yearEnd = cal.getTimeInMillis();
                    cal.add(Calendar.MONTH, -1);
                    return cal.getTimeInMillis();
                default:
                    throw new RuntimeException("Period not supported");
            }
        }
    }

    public static final long getClosestHourStartBefore(final long from) {
        return from - from % (60 * 60 * 1000);
    }

    public static final long getClosestDayStartBefore(final long from) {
        return from - from % (24 * 60 * 60 * 1000);
    }

    public static final long getClosestMonthStartBefore(final long from) {
        final Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
        cal.setTimeInMillis(from);
        cal.set(Calendar.MILLISECOND, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        return cal.getTimeInMillis();
    }
    
    public static final long getClosestYearStartBefore(final long from) {
        final Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
        cal.setTimeInMillis(from);
        cal.set(Calendar.MILLISECOND, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.MONTH, 0);
        return cal.getTimeInMillis();
    }

    public static final long getCandleStart(final Period period, final long time) {
        final long intervalInMs = period.getInterval();
        switch (period.unit) {
            case Second:
            case Minute:
                final long dayStart = getClosestDayStartBefore(time);
                return dayStart + ((time - dayStart) / intervalInMs) * intervalInMs;
            case Hour:
            case Day:
                final long monthStart = getClosestMonthStartBefore(time);
                return monthStart + ((time - monthStart) / intervalInMs) * intervalInMs;
            case Week:
                Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
                cal.setTimeInMillis(time);
                cal.set(Calendar.MILLISECOND, 0);
                cal.set(Calendar.SECOND, 0);
                cal.set(Calendar.MINUTE, 0);
                cal.set(Calendar.HOUR_OF_DAY, 0);
                cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
                if (cal.getTimeInMillis() > time) {
                    cal.add(Calendar.WEEK_OF_YEAR, -1);
                }
                return cal.getTimeInMillis();
            case Month:
                cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
                cal.setTimeInMillis(time);
                cal.set(Calendar.MILLISECOND, 0);
                cal.set(Calendar.SECOND, 0);
                cal.set(Calendar.MINUTE, 0);
                cal.set(Calendar.HOUR_OF_DAY, 0);
                cal.set(Calendar.DAY_OF_MONTH, 1);
                return cal.getTimeInMillis();
            default:
                return Long.MIN_VALUE;
        }
    }
    
    public static final String getChunkFileName(final Instrument instrument, final Period period, final OfferSide side, final long from) {
        final StringBuilder fileName = new StringBuilder();
        fileName.append(instrument.name()).append(File.separatorChar);

        final Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
        cal.setTimeInMillis(from);
        int month = cal.get(Calendar.MONTH);
        int day = cal.get(Calendar.DAY_OF_MONTH);
        fileName.append(cal.get(Calendar.YEAR)).append(File.separatorChar);
        
        if (period == Period.TICK) {
            if (month < 10) {
                fileName.append("0");
            }
            fileName.append(month).append(File.separatorChar);
            if (day < 10) {
                fileName.append("0");
            }
            fileName.append(day).append(File.separatorChar);
            int hour = cal.get(Calendar.HOUR_OF_DAY);
            if (hour < 10) {
                fileName.append("0");
            }
            fileName.append(hour).append("h").append("_").append("ticks");
        } else {
            switch (period.unit) {
                case Month:
                    fileName.append(side.name()).append("_").append("candles").append("_").append("month").append("_").append(period.numOfUnits);
                    break;
                case Week:
                    fileName.append(side.name()).append("_").append("candles").append("_").append("week").append("_").append(period.numOfUnits);
                    break;
                case Day:
                    if (month < 10) {
                        fileName.append("0");
                    }
                    fileName.append(month).append(File.separatorChar);
                    fileName.append(side.name()).append("_").append("candles").append("_").append("day").append("_").append(period.numOfUnits);
                    break;
                case Hour:
                    if (month < 10) {
                        fileName.append("0");
                    }
                    fileName.append(month).append(File.separatorChar);
                    fileName.append(side.name()).append("_").append("candles").append("_").append("hour").append("_").append(period.numOfUnits);
                    break;
                case Minute:
                    if (month < 10) {
                        fileName.append("0");
                    }
                    fileName.append(month).append(File.separatorChar);
                    if (day < 10) {
                        fileName.append("0");
                    }
                    fileName.append(day).append(File.separatorChar);
                    fileName.append(side.name()).append("_").append("candles").append("_").append("min").append("_").append(period.numOfUnits);
                    break;
                case Second:
                    if (month < 10) {
                        fileName.append("0");
                    }
                    fileName.append(month).append(File.separatorChar);
                    if (day < 10) {
                        fileName.append("0");
                    }
                    fileName.append(day).append(File.separatorChar);
                    fileName.append(side.name()).append("_").append("candles").append("_").append("sec").append("_").append(period.numOfUnits);
                    break;
                default:
                    throw new RuntimeException("Periods not supported");
            }
        }
        fileName.append(".bin");
        return fileName.toString();
    }
    

    public static final long getNextChunkStart(final Period period, final long time) {
        final long chunkStart = getChunkStart(period, time);
        if (period == Period.TICK) {
            return chunkStart + 60 * 60 * 1000;
        } else {
            switch (period.unit) {
                case Second:
                case Minute:
                    return chunkStart + 24 * 60 * 60 * 1000;
                case Hour:
                case Day:
                    Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
                    cal.setTimeInMillis(chunkStart);
                    cal.add(Calendar.MONTH, 1);
                    return cal.getTimeInMillis();
                case Week:
                case Month:
                    cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
                    cal.setTimeInMillis(chunkStart);
                    cal.add(Calendar.YEAR, 1);
                    return cal.getTimeInMillis();
                default:
                    throw new RuntimeException("Period not supported");
            }
        }
    }

    public static final long getPreviousChunkStart(final Period period, final long time) {
        final long chunkStart = getChunkStart(period, time);
        if (period == Period.TICK) {
            return chunkStart - 60 * 60 * 1000;
        } else {
            switch (period.unit) {
                case Second:
                case Minute:
                    return chunkStart - 24 * 60 * 60 * 1000;
                case Hour:
                case Day:
                    Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
                    cal.setTimeInMillis(chunkStart);
                    cal.add(Calendar.MONTH, -1);
                    return cal.getTimeInMillis();
                case Week:
                case Month:
                    cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
                    cal.setTimeInMillis(chunkStart);
                    cal.add(Calendar.YEAR, -1);
                    return cal.getTimeInMillis();
                default:
                    throw new RuntimeException("Period not supported");
            }
        }
    }

    public static final int getCandleCountInChunk(final Period period, final long time) {
        final long chunkStart = getChunkStart(period, time);
        final long chunkEnd = getChunkEnd(period, time);
        if (period == Period.TICK) {
            throw new RuntimeException("Not a candle");
        } else {
            switch (period.unit) {
                case Second:
                case Minute:
                case Hour:
                case Day:
                    return (int) ((chunkEnd - chunkStart) / period.getInterval() + 1);
                case Week:
                    Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
                    cal.setTimeInMillis(chunkStart);
                    cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
                    if (cal.getTimeInMillis() < chunkStart) {
                        cal.add(Calendar.WEEK_OF_YEAR, 1);
                    }
                    int count = 0;
                    do {
                        cal.add(Calendar.WEEK_OF_YEAR, 1);
                        ++count;
                    } while (cal.getTimeInMillis() <= chunkEnd);
                    return count;
                case Month:
                    return 12;
                default:
                    throw new RuntimeException("Period not supported");
            }
        }
    }

    public static final long getNextCandleStart(final Period period, final long candleStart) {
        switch (period.unit) {
            case Second:
            case Minute:
            case Hour:
            case Day:
            case Week:
                return candleStart + period.getInterval();
            case Month:
                final Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
                cal.setTimeInMillis(candleStart);
                cal.add(Calendar.MONTH, 1);
                return cal.getTimeInMillis();
            default:
                return Long.MIN_VALUE;
        }
    }

    public static final long getPreviousCandleStart(final Period period, final long candleStart) {
        switch (period.unit) {
            case Second:
            case Minute:
            case Hour:
            case Day:
            case Week:
                return candleStart - period.getInterval();
            case Month:
                final Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
                cal.setTimeInMillis(candleStart);
                cal.add(Calendar.MONTH, -1);
                return cal.getTimeInMillis();
            default:
                return Long.MIN_VALUE;
        }
    }

    public static final int getCandlesCountBetween(final Period period, final long fromCandleStart, final long toCandleStartIncluding) {
        switch (period.unit) {
            case Second:
            case Minute:
            case Hour:
            case Day:
            case Week:
                return (int) ((toCandleStartIncluding - fromCandleStart) / period.getInterval()) + 1;
            case Month:
                int count = 0;
                final Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
                cal.setTimeInMillis(fromCandleStart);
                while (cal.getTimeInMillis() <= toCandleStartIncluding) {
                    ++count;
                    cal.add(Calendar.MONTH, 1);
                }
                return count;
            default:
                return Integer.MIN_VALUE;
        }
    }
}

Open in new window

DynotoeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ericathomeCommented:
Based on the code sample, it might be easier to define what you want the C# to accomplish and create a simpler new set of C#.NET code rather than converting this code directly. Often that type of conversion is messy as C# has many useful methods and functions to rely on in the Framework that save lots of coding. That said and being short on time to convert code tonight, I can offer a couple of useful links.

Moving Java Applications to .NET: http://msdn.microsoft.com/en-us/library/ms973842.aspx

Converting Java Applications to Visual C#: http://msdn.microsoft.com/en-us/library/c6t6bsdk(VS.80).aspx

Sharpen, an open source converter: http://evain.net/blog/articles/2008/05/20/sharpen-an-open-source-java-to-c-converter

Eric
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DynotoeAuthor Commented:
Hi Eric,

Thanks for your input.  Like I stated, I tried the "Conversion Assistant" and could not get it installed on any machine, all with different versions of visual studio.

I need some manual help.

-D
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.