LimitLengthUtil.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. public static String toConceal(String value) {
  36. if (null == value || "".equals(value)) {
  37. return value;
  38. }
  39. int len = value.length();
  40. int pamaone = len / 2;
  41. int pamatwo = pamaone - 1;
  42. int pamathree = len % 2;
  43. StringBuilder stringBuilder = new StringBuilder();
  44. if (len <= 2) {
  45. if (pamathree == 1) {
  46. return SYMBOL;
  47. }
  48. stringBuilder.append(SYMBOL);
  49. stringBuilder.append(value.charAt(len - 1));
  50. } else {
  51. if (pamatwo <= 0) {
  52. stringBuilder.append(value.substring(0, 1));
  53. stringBuilder.append(SYMBOL);
  54. stringBuilder.append(value.substring(len - 1, len));
  55. } else if (pamatwo >= SIZE / 2 && SIZE + 1 != len) {
  56. int pamafive = (len - SIZE) / 2;
  57. stringBuilder.append(value.substring(0, pamafive));
  58. for (int i = 0; i < SIZE; i++) {
  59. stringBuilder.append(SYMBOL);
  60. }
  61. if ((pamathree == 0 && SIZE / 2 == 0) || (pamathree != 0 && SIZE % 2 != 0)) {
  62. stringBuilder.append(value.substring(len - pamafive, len));
  63. } else {
  64. stringBuilder.append(value.substring(len - (pamafive + 1), len));
  65. }
  66. } else {
  67. int pamafour = len - 2;
  68. stringBuilder.append(value.substring(0, 1));
  69. for (int i = 0; i < pamafour; i++) {
  70. stringBuilder.append(SYMBOL);
  71. }
  72. stringBuilder.append(value.substring(len - 1, len));
  73. }
  74. }
  75. return stringBuilder.toString();
  76. }
  77. /*
  78. * 随机列表
  79. * list-原始列表
  80. * count 返回长度
  81. * queryLength 查询长度
  82. * return list
  83. *
  84. * */
  85. public static <T>List<T> LimitListLengthRandomly(List<T> list,int count,int queryLength)
  86. {
  87. int truelyQueryLength=Math.min(list.size(), queryLength);
  88. if(list==null || list.size()<=0)return list;
  89. int[] randomInts=RandomUtil.generateRandomList(truelyQueryLength, count);
  90. if(list==null || list.size()<=count)return list;
  91. else {
  92. List<T>res=new ArrayList<>();
  93. for(int i=0;i<count;i++) {
  94. res.add(list.get(randomInts[i]));
  95. }
  96. return res;
  97. }
  98. }
  99. }