sana
sana

Reputation: 460

How to remove node/nodes from a collection based on condition in groovy

I am trying to remove all nodes if there is a "Action value exist 'U' and 'D' " for same date.

It should only left the list with first two nodes in below sample list as it does not have "D" action for -Date:2015-03-16 , Date:2015-03-17, Date:2015-03-22

Tried list.unique but that wont work as need to remove all if U & D both action exist.Please suggest.

[
  [UserName:test, FileName:Santande.gpg, ServerPort:21, Success:true,Date:2015-03-16 06:28:46.12, Action:U],
 [UserName:test, FileName:Santande.gpg, ServerPort:21, Success:true, Date:2015-03-17 08:27:05.157, Action:U], 
 [UserName:test, FileName:Santande.gpg, ServerPort:21, Success:true, Date:2015-03-18 08:49:37.98, Action:U], 
 [UserName:test, FileName:Santande.gpg,  ServerPort:22, Success:true, Date:2015-03-18 14:00:42.23, Action:D],
 [UserName:test, FileName:Santande.gpg, ServerPort:22, Success:true, Date:2015-03-18 15:00:42.321, Action:D], 
 [UserName:test, FileName:Santande.gpg,  ServerPort:21, Success:true, Date:2015-03-19 07:12:15.616, Action:U],
 [UserName:test, FileName:Santande.gpg, ServerPort:22, Success:true, Date:2015-03-19 08:00:47.697, Action:D],
 [UserName:test, FileName:Santande.gpg, ServerPort:22, Success:true, Date:2015-03-19 09:00:48.14, Action:D],
 [UserName:test, FileName:Santande.gpg, ServerPort:22, Success:true, Date:2015-03-19 10:00:53.163, Action:D], 
 [UserName:test, FileName:Santande.gpg, ServerPort:22, Success:true, Date:2015-03-20 09:00:47.373, Action:D], 
 [UserName:test, FileName:Santande.gpg, ServerPort:21, Success:true, Date:2015-03-20 09:08:51.637, Action:U], 
 [UserName:test, FileName:Santande.gpg, ServerPort:22, Success:true, Date:2015-03-23 08:00:37.413, Action:D], 
 [UserName:test, FileName:Santande.gpg, ServerPort:22, Success:true, Date:2015-03-23 09:00:46.423, Action:D],
 [UserName:test, FileName:Santande.gpg, ServerPort:21, Success:true, Date:2015-03-23 09:06:32.997, Action:U],
 [UserName:test, FileName:Santande.gpg, ServerPort:22, Success:true, Date:2015-03-23 10:00:50.33, Action:D]
 [UserName:test, FileName:Santande.gpg, ServerPort:22, Success:true, Date:2015-03-22 10:00:50.33, Action:U]
]

Upvotes: 0

Views: 84

Answers (1)

cfrick
cfrick

Reputation: 37008

Group by date, then pick the groups, that don't hold U and D entries. E.g.:

println([
 [UserName:'test', FileName:'Santande.gpg', ServerPort:'21', Success:'true', Date:'2015-03-16 06:28:46.120', Action:'U'],
 [UserName:'test', FileName:'Santande.gpg', ServerPort:'21', Success:'true', Date:'2015-03-17 08:27:05.157', Action:'U'], 
 [UserName:'test', FileName:'Santande.gpg', ServerPort:'21', Success:'true', Date:'2015-03-18 08:49:37.980', Action:'U'], 
 [UserName:'test', FileName:'Santande.gpg', ServerPort:'22', Success:'true', Date:'2015-03-18 14:00:42.230', Action:'D'],
].groupBy{ 
    it.Date.tokenize().first() 
}.findAll{ 
    it.value*.Action.toSet()!=['U','D'].toSet() 
}*.value.flatten())

Upvotes: 3

Related Questions