Browsing 15901 questions and answers with Jon Skeet

Using LINQ and lambdas to search Dictionary>Class>List>Struct data

given the following data source: public struct Strc { public decimal A; public decimal B; // more stuff } public class CLASS { public List<Strc> listStrc =...
Jon Skeet
people
quotationmark

You're essentially flattening to a sequence of the struct values - and that flattening is represented with SelectMany. So you want: var res = dict.Values .SelectMany(x => x.listSrc) .Where(ls => ls.A > 3) .Select(ls... more 9/30/2017 9:35:56 AM

people

C# Delegate,Action,Func ?? How to write it to make shorter code

I want to know a good idea to Rewrite my class with Lambda + delegate + Func/Action. in my code, there is lot of Lock wrapper ( to save a Sqlite model ). using...
Jon Skeet
people
quotationmark

Well you won't be able to do it quite like that, but you could write: await LockWrapper(() => Work1(modelInstance)); and write LockWrapper as: private async Task LockWrapper(Func<Task> taskProvider) { await... more 9/30/2017 9:04:09 AM

people

Date Zero Validation In Java

I have a method taking Date field as a input parameter. public static String formatDate(Date inputDate) { // if user send the date with inputDate= new Date(00000000) or new...
Jon Skeet
people
quotationmark

It sounds like you just want: if (inputDate == null || inputDate.getTime() == 0L) That will detect if inputDate is null or represents the Unix epoch. As noted in the comments though: Rejecting a single value is kinda dangerous - why... more 9/29/2017 4:39:41 PM

people

How to take care of different timezones in RemoteApp

I have a windows applications which are deployed as remoteapp. I'm trying to generate a timestamp using C# DateTime.Now. I'm interested to know, how the timestamp will be...
Jon Skeet
people
quotationmark

I'm trying to generate a timestamp using C# DateTime.Now. It's strongly recommend against doing that. That will use the time zone of the system where the code is running. It's just not suitable for a timestamp. In... more 9/29/2017 1:57:06 PM

people

How do you check if 2 OffsetDateTime lie within another 2 OffsetDateTIme?

Given an POCO Event{OffsetDateTime Start, OffsetDateTime End} and a POCO Trial {OffsetDateTime Start, OffsetDateTime End} Where trials typical span hours, and events happen over...
Jon Skeet
people
quotationmark

Note: this answer aims at "trial completely contains event" - for "trial overlaps event", see Matt Johnson's answer. OffsetDateTime.ToInstant is unambiguous, so you could certainly just convert to Instant values. You might want to create... more 9/29/2017 1:43:26 PM

people

Java 8 Date and Time: parse ISO 8601 string without colon in offset

We try to parse the following ISO 8601 DateTime String with timezone offset: final String input = "2022-03-17T23:00:00.000+0000"; OffsetDateTime.parse(input); LocalDateTime.parse(input, DateTimeFormatter.ISO_OFFSET_DATE_TIME); Both approaches fail (which makes sense as OffsetDateTime also use the DateTimeFormatter.ISO_OFFSET_DATE_TIME) because of the colon in the timezone offset. java.time.format.DateTimeParseException: Text '2022-03-17T23:00:00.000+0000' could not be parsed at index 23 But according to Wikipedia there are 4 valid formats for a timezone offset: <time>Z <time>±hh:mm <time>±hhmm <time>±hh Other frameworks/languages can parse this string without any issues, e.g. the Javascript Date() or Jacksons ISO8601Utils (they discuss this issue here) Now we could write our own DateTimeFormatter with a complex RegEx, but in my opinion the java.time library should be able to parse this valid ISO 8601 string by default as it is a valid one. For now we use Jacksons ISO8601DateFormat, but we would prefer to use the official date.time library to work with. What would be your approach to tackle this issue?
Jon Skeet
people
quotationmark

You don't need to write a complex regex - you can build a DateTimeFormatter that will work with that format easily: DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSX", Locale.ROOT); OffsetDateTime... more 9/29/2017 11:08:05 AM

people

Express 24 hours in any Java TimeUnit

If my time unit is of type seconds and cycle duration is 20 sec after 86400/20 = 4320 cycles, 24 hours are passed. long numberOfCyclesToReach24Hours(long cycleDuration, TimeUnit...
Jon Skeet
people
quotationmark

It sounds like you're just looking for the convert method: Converts the given time duration in the given unit to this unit. return unit.convert(24, TimeUnit.HOURS) / cycleDuration; more 9/28/2017 9:39:24 PM

people

Unexpected result in Enum.HasFlag

I recently encountered an error in my code where I was attempting to "dismantle" an enum flag into an array of its constituent values, but unexpected results would sometimes be...
Jon Skeet
people
quotationmark

Basically, you shouldn't use HasFlag for a non flags-based enum... and a flags-based enum should use a separate bit for each flag. The problem is indeed because of the values. Writing this in binary, you've got: public enum TestEnum { ... more 9/27/2017 2:02:23 PM

people

Unexpected behavior of WeakHashMap with Map as key returning null value after modification of key

We need to cache some information about some objects therefore we are using java.util.WeakHashMap. If we the key is java.util.HashMap we are seeing unexpected...
Jon Skeet
people
quotationmark

This has nothing to do with it being a weak map, and everything to do with you modifying a map key, which is basically something you should avoid doing. By adding an entry to the map, you're changing its hash code. This is easily... more 9/27/2017 9:17:00 AM

people

How do I exclude a namespace from dotcover using xml?

I would like to exclude some namespaces from my test coverage statistics. We're using xml for our dotcover configuration. For example, we exclude our test projects this...
Jon Skeet
people
quotationmark

Well, you can use a ClassMask, if you don't mind "sub-namespaces" being excluded too. Suppose you want to expose Foo.Bar, you could have: <FilterEntry> <ClassMask>Foo.Bar.*</ClassMask> </FilterEntry> If you... more 9/25/2017 2:05:22 PM

people