Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package libepg.ts.packet; import epgtools.reverselookupmapfactory.ReverseLookUpMapFactory; import java.text.MessageFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import java.util.function.Function; import org.apache.commons.lang3.Range; /** * TS???PID???????????? 0x0000=PAT 0x0001=CAT 0x00020x000F= * 0x0010=NIT 0x00110x1FFE=PAT?CAT?NIT?????????? 0x1FFF= * * (?????????PAT???????????)<br> * 0x0,0x1,0x10,0x11,0x12,0x14,0x23,0x24,0x27,0x28,0x29,0x100,0x110,0x111,0x130,0x137,0x138,0x140,0x160,0x161,0x162,0x163,0x164,0x165,0x1f0,0x1ff,0x238,<br> * 0x3f0,0x480,0x481,0x4f0,0x580,0x581,0x583,0x587,0x589,0x58a,0x58b,0x5a6,0x5f4,0x5ff,0x781,0x900,0x901,0x902,0xaef,0xb93,0xe52,0x1380,0x161a,0x1c61,<br> * 0x1c63,0x1d76,0x1df0,0x1f22,0x1f86,0x1fc8,0x1ffd)<br> */ public enum PROGRAM_ID { /** * PAT(Program Association Table) MPEG-2 * TS???????????????MPEG-2 TS <br> * PMT?PID????<br> * PAT?PID?0????<br> */ PAT("PAT", 0x0), UNDEFINED("", 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf), CAT("CAT", 0x1), /** * ?(?????) */ NIT("?", 0x10), /** * SDTService Description Table<br> * ??????????????????????id=0x42<br> * * BAT(SDT??ID?????????)<br> * ????????????????????(?????)id=?<br> */ SDT_OR_BAT("????", 0x11), /** * ????????????? (?=0x12 =0x26,0x27) */ EIT_GR_ST("", 0x12, 0x26, 0x27), /** * null */ NULL_PACKET("null", 0x1FFF); /** * ?? */ private static final Map<Integer, PROGRAM_ID> rev; private static final Function<PROGRAM_ID, Set<Integer>> func1 = (PROGRAM_ID t) -> t.getPids(); //??? private static final ReverseLookUpMapFactory<Integer, Set<Integer>, PROGRAM_ID> revmapf = new ReverseLookUpMapFactory<>( func1); static { for (PROGRAM_ID pid : PROGRAM_ID.values()) { revmapf.put(pid); } rev = revmapf.getDict(); } ; /** * ID?????? * * @param pid ID * @return ID????null */ public static synchronized PROGRAM_ID reverseLookUp(int pid) { return rev.get(pid); } private final String pidName; private final Set<Integer> pids; private PROGRAM_ID(String pidName, Integer pid, Integer... pids) { this.pidName = pidName; if ((this.pidName == null) || ("".equals(this.pidName))) { throw new IllegalArgumentException("???????????"); } List<Integer> t = new ArrayList<>(); if (pid != null) { t.add(pid); } else { throw new NullPointerException("PID??????"); } if (pids != null) { t.addAll(Arrays.asList(pids)); } Range<Integer> r = Range.between(0x0000, 0x1FFF); for (Integer i : t) { if (!r.contains(i)) { MessageFormat msg = new MessageFormat("PID????PID={0}"); Object[] parameters = { Integer.toHexString(i) }; throw new IllegalArgumentException(msg.format(parameters)); } } Set<Integer> temp = Collections.synchronizedSet(new HashSet<Integer>()); temp.addAll(t); this.pids = Collections.unmodifiableSet(temp); } /** * ??????PID??????? * * @param pid PID * @return ?????true */ public synchronized boolean contains(int pid) { return this.pids.contains(pid); } public String getPidName() { return pidName; } /** * @return ??????pid? */ public Set<Integer> getPids() { return pids; } @Override public synchronized String toString() { StringBuilder s = new StringBuilder(); for (int i : this.pids) { s.append("["); s.append(Integer.toHexString(i)); s.append("]"); } String set = s.toString(); MessageFormat msg = new MessageFormat("{0}(pidName={1},PIDs={2})"); Object[] parameters = { super.toString(), this.getPidName(), set }; return msg.format(parameters); } }