Software Construction Series(2)

本文探讨了Java中Collections.unmodifiableList方法的作用及其实现的不可变性级别,并通过实例演示了如何使用该方法来创建不可修改的列表。指出为了确保数据的安全性,除了使集合不可变外,还需确保其元素也不可变。

Collections.unmodifiable*()与Rep Exposure

JDK在collections接口中提供了一类静态函数:collections.unmodifiable*(),其中*匹配Collections的子类。

如图:

1339509-20180402205314930-1046247535.png

简而言之:这类函数返回一个相应对象的不可更改的Copy。

问题在于不可更改性到那种程度:

1、相当于final?(即reference不可变)

2、相当于imutable?(即集合本身内容不可变,其中元素仍可变)

3、相当于can't do anything?(即集合本身不可变,其中元素不可变)

做了如下测试:

package tests;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class list_test {
  List<Pair> list = new ArrayList<>();

  public void add(Pair a) {
    list.add(a);
  }

  public Pair get(int i) {
    return list.get(i);
  }

  public List<Pair> getUnmodified() {
    return Collections.unmodifiableList(list);
  }



  public static void main(String[] args) {
    list_test a = new list_test();

    Pair x = new Pair(1, 1);
    a.add(x);

    List<Pair> u = a.getUnmodified();
    u.get(0).Set(0, 0);
    System.out.println(u.get(0).getX() + "," + u.get(0).getY());
    System.out.println(a.get(0).getX() + "," + a.get(0).getY());

    try {
      u.add(x);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}


class Pair {
  private int x;
  private int y;

  public Pair(int x, int y) {
    this.x = x;
    this.y = y;
  }


  public void Set(int x, int y) {
    this.x = x;
    this.y = y;
  }

  public int getX() {
    return x;
  }

  public int getY() {
    return y;
  }
}

Output:

0,0
0,0
java.lang.UnsupportedOperationException
    at java.util.Collections$UnmodifiableCollection.add(Collections.java:1055)
    at tests.list_test.main(list_test.java:36)

分析:

unmodifiable的效果为2,相当于imutable(即集合本身内容不可变,其中元素仍可变)。

所以对于要防止数据泄露, 不仅要返回一个unmodifiable的集合对象,还需要其中的元素也是imutable的才行。

转载于:https://www.cnblogs.com/KarlZhang/p/8697369.html

rts Command iwconfig athX rts Description Enable RTS during PPDU construction, based on PPDU size. Sets the minimum packet size for which RTS/CTS protection is used. This setting is used to reduce the amount of arbitration that occurs with short packet transmission, improving throughput. The value of minpktsize is set to the minimum packet size for which to use the RTS/CTS handshake. Setting minpktsize to a value of 0 disables RTS/CTS handshake entirely. The use of RTS/CTS in 802.11n is governed by rate tables and other settings, so this command may not have the desired effect when using 802.11n rates. The threshold should be more than 256 B (as defined by iwconfig). Example usage: iwconfig rts 256 g_enablertscts iwpriv athX g_enablertscts Status of use of RTS/CTS when first transmission attempt fails. rc_retries iwpriv athX rc_retries get_rc_retries iwpriv athX get_rc_retries bcast_rate iwpriv athX bcast_rate get_bcast_rate iwpriv athX get_bcast_rate vht_txmcsmap iwpriv athX vht_txmcsmap g_vht_txmcsmap Configures the number of retries for rate control. For a direct attach data path, software must select a rate series, which consists of four rates and the number of attempts (retries) at each rate. The rate series is used by the WLAN MAC hardware. The MAC hardware first attempts to transmit at the rate of the first entry in the series. If it fails, the transmission is repeated at that rate for a total number of attempts equal to the number of retries specified in the series. If all attempts at that rate fail, the MAC hardware drops down to the next rate in the series and repeats. Displays the number of retries for rate control. Specifies the broadcast rate of transmission. Displays the configured broadcast rate of transmission. Specifies the Tx MCS Map field of VHT Capabilities IE. iwpriv athX g_vht_txmcsmap Displays the Tx MCS Map field of VHT Capabilities IE. vht_rxmcsmap iwpriv athX vht_rxmcsmap Specifies the Rx MCS Map field of VHT Capabilities IE. g_vht_rxmcsmap iwpriv athX g_vht_rxmcsmap Displays the Rx MCS Map field of VHT Capabilities IE. get_parent iwpriv athX get_parent set_onetxchain iwpriv athX set_onetxchain set_cabq_maxdur Retrieves the parent AP, which is the device to which the client is actually connected. Parent Wi-Fi of an AP is the wifi which the user originally configures as extender of the AP. Specifies that only one Tx chain be used for transmission. iwpriv athX set_cabq_maxdur Specifies the maximum period of time for the content after beacon queue (CABQ). get_acs_state iwpriv athX get_acs_state get_cac_state iwpriv athX get_cac_state implicitbf iwpriv athX implicitbf get_implicitbf iwpriv athX get_implicitbf Retrieves the state of the automatic channel selection (ACS) reporting functionality. Retrieves the status of the channel availability check (CAC) mechanism. Enable (1) or disable (0) implicit beamforming. Retrieves the configuration of implicit beamforming.
09-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值