Browsing 7239 questions and answers with Jon Skeet

private final vs public final fields in immutable objects (java)

Item 15 in Effective Java states that it's recommended to use private final fields as against public final fields in an immutable object as it might prevent changing the internal...
Jon Skeet
people
quotationmark

It's not a matter of just changing access specifier - you might change everything about the internal representation. Suppose you have three byte values as part of the state of your object. You could store them in a single int field, or... more 1/17/2015 11:22:29 AM

people

Can I perform a nullable struct equality test without boxing?

I have a struct Foo : IEquatable<Foo>. I'm trying to write: bool isEqual = a.Equals(b); Where a and b are both of the type Foo?. This code works, but b is being boxed! Is...
Jon Skeet
people
quotationmark

You can call the static Nullable.Equals method: bool isEqual = Nullable.Equals(a, b); more 1/16/2015 10:43:49 PM

people

Determine if the time an a certain timezone is within a range

My company has support staff that are available from 7am to 7pm CST, and we try to reflect that on our website by showing either a button to initiate a chat, or a message saying...
Jon Skeet
people
quotationmark

You're doing far more than you need to. Personally I'd use my Noda Time project, but all of this can be done fairly easily with the BCL, using TimeZoneInfo.ConvertTime: var zone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard... more 1/16/2015 9:10:08 PM

people

Convert String to Joda LocalTime format (HH:mm:ss) and Remove milliseconds

DateTimeFormatter fmt = DateTimeFormat.forPattern("HH:mm:ss"); LocalTime localTime = fmt.parseLocalTime("02:51:20"); System.out.println("LocalTime: "+localTime); I...
Jon Skeet
people
quotationmark

Yes - you just need to format when you print out. Currently you're just using the default toString() representation. If you're happy with the formatter you've already got, you can just use: System.out.println("LocalTime: " +... more 1/16/2015 5:33:18 PM

people

How do I get typeArguments from a List<T> returned by object.getClass().getDeclaredField("fieldname").getGenericType();

I am working on this problem for about 2 days and I still can not solve it. I have this the method: public List<T> findByKeyValue(String key, String value, Object t) { ...
Jon Skeet
people
quotationmark

You should be able to just cast to ParameterizedType: Type type = field.getGenericType(); if (type instanceof ParameterizedType) { ParamterizedType pt = (ParameterizedType) type; if (pt.getRawType() == List.class && ... more 1/16/2015 5:25:15 PM

people

How to make my Action working?

I have code below: I get error like "Operator '.' cannot be applied to operand of type 'void'". Is there anybody can explan the reason and modify the code please? void Main() { ...
Jon Skeet
people
quotationmark

This is the problem: ....ForEach(Modify).Dump ForEach returns void. You could rewrite it as: var list = Claims.AsEnumerable() .Select((claim, no) => Populate(claim, no)) ... more 1/16/2015 4:15:36 PM

people

Constructor on Generic Class has Invalid Arguments

I'm looking to use the tips from this article to push the code coverage on a project of mine closer to 100%. Specifically the last section which outlines an idea for a workaround...
Jon Skeet
people
quotationmark

This is the problem: public EnsureCodeCoverageAwaiter<T> GetAwaiter<T>() You don't want that to be a generic method introducing a new type parameter T - you just want to use the type parameter of the type. So you... more 1/16/2015 3:14:25 PM

people

Convert fulltime to minutes C#

I need convert GMT time 14:21:34 to minute (14*60+21=861 minutes) I know there is a inbuilt function which convert min to HH:MM Use TimeSpan.FromMinutes: var result =...
Jon Skeet
people
quotationmark

Two options to get to a TimeSpan: 1) Parse it as a DateTime (specifying a format), then use DateTime.TimeOfDay to get a TimeSpan var dt = DateTime.ParseExact( "14:21:34", @"HH\:mm\:ss", ... more 1/16/2015 2:37:42 PM

people

Get generic parameter of interface implementation

I have the following interface: interface Nofifier<T> { }. I have the following implementation: class MyClass implements Notifier<String>, Notifier<Integer> {...
Jon Skeet
people
quotationmark

Yes - you can call Class.getGenericInterfaces(), which returns a Type[]. That includes type argument information. Complete example: import java.lang.reflect.Type; interface Notifier<T> { } class Foo implements... more 1/16/2015 1:57:06 PM

people

Is this a valid date format 2007 12 13+01:00?

We have a very strange situation. We expect some data from web service and then we process it in java code. Suddenly, it started to fail. We found out, that we receive the date...
Jon Skeet
people
quotationmark

I don't believe it's a valid ISO-8601 (or RFC 3339) format, which is what's usually used for web services - that only appears to include time zone offsets when a time is specified as well. However, as noted in comments, it is a valid XML... more 1/16/2015 1:10:40 PM

people