LimitLengthUtil.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package com.goafanti.common.utils;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import org.thymeleaf.expression.Lists;
  5. import com.goafanti.demand.bo.DemandListBo;
  6. public class LimitLengthUtil {
  7. //科技需求首页
  8. public static void limiteDemandLength(List<DemandListBo> list,int titleLength,int descripLength) {
  9. for(DemandListBo demandListBo:list) {
  10. demandListBo.setName(limitLengthAndAddPoints(demandListBo.getName(),titleLength ));
  11. demandListBo.setProblemDes(limitLengthAndAddPoints(demandListBo.getProblemDes(), descripLength));
  12. }
  13. }
  14. public static void limiteDemandCompanyAndName(List<DemandListBo> list,int titleLength,int descripLength) {
  15. for(DemandListBo demandListBo:list) {
  16. demandListBo.setName(limitLengthAndAddPoints(demandListBo.getName(),titleLength ));
  17. demandListBo.setEmployerName(limitLengthAndAddPoints(toConceal(demandListBo.getEmployerName()), descripLength));
  18. }
  19. }
  20. public static String limitLengthAndAddPoints(String s,int l) {
  21. if(s!=null) {
  22. if(s.length()>l) {
  23. String reString=s.substring(0, l);
  24. StringBuilder stringBuilder=new StringBuilder(reString);
  25. stringBuilder.append("...");
  26. return stringBuilder.toString();
  27. }
  28. else return s;
  29. }
  30. return s;
  31. }
  32. //脱敏
  33. private static final int SIZE = 8;
  34. private static final String SYMBOL = "*";
  35. @SuppressWarnings("unused")
  36. public static String toConceal(String value) {
  37. if (null == value || "".equals(value)) {
  38. return value;
  39. }
  40. int len = value.length();
  41. int pamaone = len / 2;
  42. int pamatwo = pamaone - 1;
  43. int pamathree = len % 2;
  44. StringBuilder stringBuilder = new StringBuilder();
  45. if (len <= 2) {
  46. if (pamathree == 1) {
  47. return SYMBOL;
  48. }
  49. stringBuilder.append(SYMBOL);
  50. stringBuilder.append(value.charAt(len - 1));
  51. } else {
  52. if (pamatwo <= 0) {
  53. stringBuilder.append(value.substring(0, 1));
  54. stringBuilder.append(SYMBOL);
  55. stringBuilder.append(value.substring(len - 1, len));
  56. } else if (pamatwo >= SIZE / 2 && SIZE + 1 != len) {
  57. int pamafive = (len - SIZE) / 2;
  58. stringBuilder.append(value.substring(0, pamafive));
  59. for (int i = 0; i < SIZE; i++) {
  60. stringBuilder.append(SYMBOL);
  61. }
  62. if ((pamathree == 0 && SIZE / 2 == 0) || (pamathree != 0 && SIZE % 2 != 0)) {
  63. stringBuilder.append(value.substring(len - pamafive, len));
  64. } else {
  65. stringBuilder.append(value.substring(len - (pamafive + 1), len));
  66. }
  67. } else {
  68. int pamafour = len - 2;
  69. stringBuilder.append(value.substring(0, 1));
  70. for (int i = 0; i < pamafour; i++) {
  71. stringBuilder.append(SYMBOL);
  72. }
  73. stringBuilder.append(value.substring(len - 1, len));
  74. }
  75. }
  76. return stringBuilder.toString();
  77. }
  78. /*
  79. * 随机列表
  80. * list-原始列表
  81. * count 返回长度
  82. * queryLength 查询长度
  83. * return list
  84. *
  85. * */
  86. public static <T>List<T> LimitListLengthRandomly(List<T> list,int count,int queryLength)
  87. {
  88. int truelyQueryLength=Math.min(list.size(), queryLength);
  89. count=Math.min(count, truelyQueryLength);
  90. if(list==null || list.size()<=0)return list;
  91. int[] randomInts=RandomUtil.generateRandomList(truelyQueryLength, count);
  92. if(list==null || list.size()<=count)return list;
  93. else {
  94. List<T>res=new ArrayList<>();
  95. for(int i=0;i<count;i++) {
  96. res.add(list.get(randomInts[i]));
  97. }
  98. return res;
  99. }
  100. }
  101. }