Browsing 7239 questions and answers with Jon Skeet

Writing JUnit tests for more complex methods

I am just learning JUnit testing procedures and have gone through all the tutorials I could find. I understand the concept of implementing basic tests, such as when a method takes...
Jon Skeet
people
quotationmark

However, what does one do when there is a more complex method to test, such as perhaps one that uses numbers generated from a Random object You find a way of controlling that Random. For example, you could pass it into the method, and... more 5/22/2014 6:29:20 PM

people

Simple short Encrypt string to fixed length n matter what the input string length is

I would like to encrypt a string and it has to be decrypted back. The input string could be of varying length but the encrypted string must be a max of 15 characters and...
Jon Skeet
people
quotationmark

The input string could be of varying length but the encrypted string must be a max of 15 characters and alphanumeric. This is clearly impossible. If you solved this, you would solve all possible storage concerns - after all, you'd be... more 5/22/2014 5:52:53 PM

people

Passing value parameter to Task in c#

I have an issue with passing a long by value to a Task. I have a list of ID's where I loop through each one, assign to a local variable then pass as a parameter to a new Task. I...
Jon Skeet
people
quotationmark

Any ideas how you can guarantee the value passed to the task without waiting for the task to complete? Sure - just create a separate variable which isn't modified anywhere else. You can use a new scope to make that clear: long num =... more 5/22/2014 5:36:35 PM

people

String to double with precision

I do have a String that is read from the file with the value: 38.739793110376837 When i convert this value to double using: double.parseDouble("38.739793110376837"); I get the...
Jon Skeet
people
quotationmark

How can i have the original value in a double variable? You can't. The closest double to your original value is exactly 38.739793110376837148578488267958164215087890625 That's being converted to "38.73979311037684" because that's the... more 5/22/2014 4:49:04 PM

people

What is the most efficient way signal to multiple consumer threads that there is data available from a single producer thread?

I have a data source which will provide data regularly for N listeners. I can control access to the data using a ReaderWriterLock but what should be used to signal all listeners...
Jon Skeet
people
quotationmark

You could use a ManualResetEvent instead... but I would instead encourage you to use a higher level abstraction. Consider whether your task may be appropriate for the Dataflow API - or possibly use a BlockingCollection for each listener,... more 5/22/2014 4:34:31 PM

people

Enum implementing interface, interface and method visibility

I just came accross the following code, which surprised me a little bit, I converted it to a simple SSCEE here though: custompackage.package1.MyEnum.java public enum MyEnum...
Jon Skeet
people
quotationmark

Well you can't see MyInterface from outside the package, as you said - but MyEnum effectively has a public abstract myMethod() method, which you're able to use as a method reference. Leaving aside fancy new Java 8 features, this is valid... more 5/22/2014 3:49:05 PM

people

what will happen if fields with same name inherites from two sources(class and interface)

valid code: interface Int1{ String str = "123"; } class Pparent{ String str = "123"; } class F extends Pparent implements Int1{ } invalid code add main method...
Jon Skeet
people
quotationmark

There are two fields which the identifier str can refer to - a static one inherited from Int1, and an instance one inherited from Pparent. Even though it's invalid to try to use the instance one from a static context, the name can still be... more 5/22/2014 3:10:30 PM

people

Java Switch Statememt Constant Expression Required Using Public Enum from Abstract Class

I have written code working with a protocol I have design on top of websockets for a project creating an Android App. I have a class which handles the websocket communication...
Jon Skeet
people
quotationmark

As you've seen, calling a method doesn't count as a constant expression. The cleaner solution (IMO) is to have a static method within your enum: // Enum renamed to comply with conventions public enum MessageType { private static... more 5/22/2014 1:52:36 PM

people

How do I validate an enum parameter on a public method?

I have this extension method for an Enum: public static List<Enum> Values(this Enum theEnum) { return...
Jon Skeet
people
quotationmark

I can't check for null because an enum is a non-nullable value type. Any particular enum is a value type, but Enum itself isn't. (Just like ValueType isn't a value type either... every type derived from ValueType except Enum is a... more 5/22/2014 1:36:09 PM

people

What would you do here? Return a null or throw an exception (framework design guides)

I'm developing a C# .NET Framework 4.0 library. I have this code: public static byte GetBatchStatus(string connString) { if (string.IsNullOrEmpty(connString)) throw...
Jon Skeet
people
quotationmark

If that value is not on database is an error. An error in terms of "my belief system about the state of the system has been violated" or "the input must be invalid somehow"? It sounds like it's more the former - so I'd throw an... more 5/22/2014 12:49:26 PM

people