|
32 | 32 | import java.util.Set; |
33 | 33 | import java.util.TimeZone; |
34 | 34 |
|
| 35 | +import org.apache.commons.collections.CollectionUtils; |
| 36 | +import org.apache.commons.collections.MapUtils; |
35 | 37 | import org.apache.commons.lang.StringUtils; |
| 38 | +import org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyResource; |
36 | 39 |
|
37 | 40 | public class StringUtil { |
38 | 41 |
|
@@ -338,4 +341,174 @@ public static List<String> getURLs(String configURLs) { |
338 | 341 | } |
339 | 342 | return configuredURLs; |
340 | 343 | } |
| 344 | + |
| 345 | + public static Map<String, Map<String, String>> dedupStringsMapOfMap(Map<String, Map<String, String>> value, Map<String, String> strTbl) { |
| 346 | + final Map<String, Map<String, String>> ret; |
| 347 | + |
| 348 | + if (MapUtils.isNotEmpty(value)) { |
| 349 | + ret = new HashMap<>(value.size()); |
| 350 | + |
| 351 | + for (Map.Entry<String, Map<String, String>> entry : value.entrySet()) { |
| 352 | + ret.put(dedupString(entry.getKey(), strTbl), dedupStringsMap(entry.getValue(), strTbl)); |
| 353 | + } |
| 354 | + } else { |
| 355 | + ret = value; |
| 356 | + } |
| 357 | + |
| 358 | + return ret; |
| 359 | + } |
| 360 | + |
| 361 | + public static Map<String, Set<String>> dedupStringsMapOfSet(Map<String, Set<String>> value, Map<String, String> strTbl) { |
| 362 | + final Map<String, Set<String>> ret; |
| 363 | + |
| 364 | + if (MapUtils.isNotEmpty(value)) { |
| 365 | + ret = new HashMap<>(value.size()); |
| 366 | + |
| 367 | + for (Map.Entry<String, Set<String>> entry : value.entrySet()) { |
| 368 | + ret.put(dedupString(entry.getKey(), strTbl), dedupStringsSet(entry.getValue(), strTbl)); |
| 369 | + } |
| 370 | + } else { |
| 371 | + ret = value; |
| 372 | + } |
| 373 | + |
| 374 | + return ret; |
| 375 | + } |
| 376 | + |
| 377 | + public static Map<String, List<String>> dedupStringsMapOfList(Map<String, List<String>> value, Map<String, String> strTbl) { |
| 378 | + final Map<String, List<String>> ret; |
| 379 | + |
| 380 | + if (MapUtils.isNotEmpty(value)) { |
| 381 | + ret = new HashMap<>(value.size()); |
| 382 | + |
| 383 | + for (Map.Entry<String, List<String>> entry : value.entrySet()) { |
| 384 | + ret.put(dedupString(entry.getKey(), strTbl), dedupStringsList(entry.getValue(), strTbl)); |
| 385 | + } |
| 386 | + } else { |
| 387 | + ret = value; |
| 388 | + } |
| 389 | + |
| 390 | + return ret; |
| 391 | + } |
| 392 | + |
| 393 | + public static HashMap<String, List<String>> dedupStringsHashMapOfList(HashMap<String, List<String>> value, Map<String, String> strTbl) { |
| 394 | + final HashMap<String, List<String>> ret; |
| 395 | + |
| 396 | + if (MapUtils.isNotEmpty(value)) { |
| 397 | + ret = new HashMap<>(value.size()); |
| 398 | + |
| 399 | + for (Map.Entry<String, List<String>> entry : value.entrySet()) { |
| 400 | + ret.put(dedupString(entry.getKey(), strTbl), dedupStringsList(entry.getValue(), strTbl)); |
| 401 | + } |
| 402 | + } else { |
| 403 | + ret = value; |
| 404 | + } |
| 405 | + |
| 406 | + return ret; |
| 407 | + } |
| 408 | + |
| 409 | + public static Map<String, Object> dedupStringsMapOfObject(Map<String, Object> value, Map<String, String> strTbl) { |
| 410 | + final Map<String, Object> ret; |
| 411 | + |
| 412 | + if (MapUtils.isNotEmpty(value)) { |
| 413 | + ret = new HashMap<>(value.size()); |
| 414 | + |
| 415 | + for (Map.Entry<String, Object> entry : value.entrySet()) { |
| 416 | + ret.put(dedupString(entry.getKey(), strTbl), entry.getValue()); |
| 417 | + } |
| 418 | + } else { |
| 419 | + ret = value; |
| 420 | + } |
| 421 | + |
| 422 | + return ret; |
| 423 | + } |
| 424 | + |
| 425 | + public static Map<String, RangerPolicyResource> dedupStringsMapOfPolicyResource(Map<String, RangerPolicyResource> value, Map<String, String> strTbl) { |
| 426 | + final Map<String, RangerPolicyResource> ret; |
| 427 | + |
| 428 | + if (MapUtils.isNotEmpty(value)) { |
| 429 | + ret = new HashMap<>(value.size()); |
| 430 | + |
| 431 | + for (Map.Entry<String, RangerPolicyResource> entry : value.entrySet()) { |
| 432 | + RangerPolicyResource resource = entry.getValue(); |
| 433 | + |
| 434 | + resource.dedupStrings(strTbl); |
| 435 | + |
| 436 | + ret.put(dedupString(entry.getKey(), strTbl), resource); |
| 437 | + } |
| 438 | + } else { |
| 439 | + ret = value; |
| 440 | + } |
| 441 | + |
| 442 | + return ret; |
| 443 | + } |
| 444 | + |
| 445 | + public static Map<String, String> dedupStringsMap(Map<String, String> value, Map<String, String> strTbl) { |
| 446 | + final Map<String, String> ret; |
| 447 | + |
| 448 | + if (MapUtils.isNotEmpty(value)) { |
| 449 | + ret = new HashMap<>(value.size()); |
| 450 | + |
| 451 | + for (Map.Entry<String, String> entry : value.entrySet()) { |
| 452 | + ret.put(dedupString(entry.getKey(), strTbl), dedupString(entry.getValue(), strTbl)); |
| 453 | + } |
| 454 | + } else { |
| 455 | + ret = value; |
| 456 | + } |
| 457 | + |
| 458 | + return ret; |
| 459 | + } |
| 460 | + |
| 461 | + public static Set<String> dedupStringsSet(Set<String> value, Map<String, String> strTbl) { |
| 462 | + final Set<String> ret; |
| 463 | + |
| 464 | + if (CollectionUtils.isNotEmpty(value)) { |
| 465 | + ret = new HashSet<>(value.size()); |
| 466 | + |
| 467 | + for (String val : value) { |
| 468 | + ret.add(dedupString(val, strTbl)); |
| 469 | + } |
| 470 | + } else { |
| 471 | + ret = value; |
| 472 | + } |
| 473 | + |
| 474 | + return ret; |
| 475 | + } |
| 476 | + |
| 477 | + public static List<String> dedupStringsList(List<String> value, Map<String, String> strTbl) { |
| 478 | + final List<String> ret; |
| 479 | + |
| 480 | + if (CollectionUtils.isNotEmpty(value)) { |
| 481 | + ret = new ArrayList<>(value.size()); |
| 482 | + |
| 483 | + for (String val : value) { |
| 484 | + ret.add(dedupString(val, strTbl)); |
| 485 | + } |
| 486 | + } else { |
| 487 | + ret = value; |
| 488 | + } |
| 489 | + |
| 490 | + return ret; |
| 491 | + } |
| 492 | + |
| 493 | + public static Collection<String> dedupStringsCollection(Collection<String> value, Map<String, String> strTbl) { |
| 494 | + final Collection<String> ret; |
| 495 | + |
| 496 | + if (CollectionUtils.isNotEmpty(value)) { |
| 497 | + ret = value instanceof Set ? new HashSet<>(value.size()) : new ArrayList<>(value.size()); |
| 498 | + |
| 499 | + for (String val : value) { |
| 500 | + ret.add(dedupString(val, strTbl)); |
| 501 | + } |
| 502 | + } else { |
| 503 | + ret = value; |
| 504 | + } |
| 505 | + |
| 506 | + return ret; |
| 507 | + } |
| 508 | + |
| 509 | + public static String dedupString(String str, Map<String, String> strTbl) { |
| 510 | + String ret = str != null ? strTbl.putIfAbsent(str, str) : null; |
| 511 | + |
| 512 | + return ret == null ? str : ret; |
| 513 | + } |
341 | 514 | } |
0 commit comments